add commit button to same fieldset as inputs in formtastic - ruby-on-rails-3

How do I add my submit button into the same li as the inputs of my form?
= semantic_form_for :regression_test_environments do |env|
%ul.input-list
= env.inputs :name, :navigation, :url, :title => "New Environment"
%li.commit= env.submit "Save Environment", :class => "submit"
This just puts the li outside of the fieldset with class inputs. Is there a way to include it in the fieldset(and ul)?

Does this work:
= semantic_form_for :regression_test_environments do |env|
%ul.input-list
= env.inputs :name => "New Environment" do
= env.input :name
= env.input :navigation,
= env.input :url
%li.commit= env.submit "Save Environment", :class => "submit"
I am not sure if you have a field :title or you want to name your fieldset (in which case you need to use :name).

Related

How to show two 'f.input' in one line in activeadmin form page

form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option
f.input :score
end
f.actions
end
In above form, how to show this two inputs
f.input :option
f.input :score
in one line? Any idea?
I think I got the answer
First we should check this Formtastic::Helpers::InputHelper
According to the helper:
input is used to render all content (labels, form widgets, error messages, hints, etc) for a single form input (or field), usually representing a single method or attribute on the form's object or model.
The content is wrapped in an li tag, so it's usually called inside an inputs block (which renders an ol inside a fieldset).
It's option :wrapper_html can be used to override or add to the HTML attributes to be passed down to the wrapping li tag
So in the form we should do this:
form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option, :wrapper_html => { :class => 'fl' }
f.input :score, :wrapper_html => { :class => 'fl' }, :label => false
end
f.actions
end
:label => false can disabled the label of form input.
And then specify the class 'fl' in the css file(I just add following css from Include two inputs in same LI element in Formtastic in active_admin.css.scss like #Andrey Deineko told):
form.formtastic fieldset ol li.fl {display:inline;}
Then we have two inputs on the same line~Hope this will be helpful~

How pass variable submitted in form into 'new' action

I am trying to send a message to another user. But when I leave a field blank, the controller renders another 'new' action.
Here is the view code that links to the first submit:
<%= link_to 'Send Mail', {:controller => 'messages', :action => 'new', :recipient_id => #profile.id}, :class => 'btn btn-large btn-primary' %>
Here is the message#new code:
def new
#message = Message.new
#recipient = Profile.find(params[:recipient_id])
end
Here is the message#new view code:
<%= simple_form_for #message do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.input :recipient_id, :as => :hidden, :input_html => {:value => #recipient.id} %>
<%= f.input :sender_id, :as => :hidden, :input_html => {:value => #recipient.id} %>
<%= f.button :submit %>
<% end %>
Here is the message#create code:
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "New message created"
redirect_to #message
else
flash[:error] = "Did not work"
#recipient = params[:recipient_id]
render action: "new"
end
end
How do I get the :recipient_id value submitted in the form and pass into the render new action?
When you rendering new method rails won't execute it codes instead it only renders the view(new.html.erb) of the new action and you already generating
a recipient_id which is enough for your form to pass the parameter
Try this
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "New message created"
redirect_to #message
else
flash[:error] = "Did not work"
#recipient = Profile.find(params[:message][:recipient_id])
render action: "new"
end
end
When rendering the new action we have to create the instances manually which is used in the view.
Note: If you want generate a new parameter use redirect_to method, where render uses the previous parameters until a new request is generated

How to display only the value in edit page in Active Admin

I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end

2 level nested form with acts_as_commentable and nested_form gems

I am using nested_form and acts_as_commentable
I want to be able to add comments to either a Question or a Solution. A Question has many Solutions (think stackoverflow and you won't be far wrong).
For the nested form below how do I pass Solution.comments to the link_to_add helper (see #comment 4th line from end)? Currently comments entered below specific Solutions are assigned to the parent Question (on submit), even though the helper is embedded in the fields_for of the Solution fields.
(NOTE: if I add comments via Solution.comments.create in the rails console the comment is displayed correctly under the solution when I redisplay so I am pretty sure that it is just the link_to_add helper call that I need to fix.
Anyone got any ideas?
Here is the (haml) nested form
= nested_form_for #question, :html => { :class => 'form' } do |f|
.control-group
= f.fields_for :comments do |comment_form|
= comment_form.text_field :comment
= comment_form.link_to_remove "Remove Comment"
%p= f.link_to_add "Add a comment", :comments
.form-actions
= f.submit nil, :class => 'btn btn-primary'
.control-group
= f.fields_for :solutions do |solution_form|
= solution_form.text_area :solution
= solution_form.link_to_remove "Remove Solution"
%br
= solution_form.fields_for :comments do |comments_form|
= comments_form.text_field :comment
= comments_form.link_to_remove "Remove Comment"
# I believe the below line is the problem
%p= solution_form.link_to_add "Add Comment", :comments
%p= f.link_to_add "Add Solution", :solutions
.form-actions
= f.submit nil, :class => 'btn btn-primary'

Adding name value with link_to

I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>