I can't find how to display a dynamic label in Rails, i've tried using the :value => show_name property but it didn't work, it only displays Show name. Here is the view code
<p>
<div class="control-group">
<%= f.label :show_name, :value => :show_name, :class => 'control-label' %>
<%= #this next line fails with undefined method `show_name' for #<ActionView::Helpers::FormBuiler>
#f.label f.send :show_name, :class => 'control-label'
%>
<div class="controls">
<%= f.text_field :variable_value, :class => 'text_field' %>
<%= f.hidden_field :variable_id, :class => 'text_field' %>
<%= f.hidden_field :show_name, :class => 'text_field' %>
</div>
</div>
<p>
and if needed here is the show_name definition inside my model.
def show_name
Variable.find_by_id(self.variable_id).name
end
Ok, so i end up finding a solution that is very DRY, thank to this post. And the only thing im going to do is explain a lit bit more what to do:
First we are going to asume the most complex case in which we have nested forms and so we are using fields_for inside a form_for method:
<!-- f represents the form from `form_for` -->
<%= f.fields_for :nested_model do |builder| %>
<p>
<div class="control-group">
<!-- here we are just calling a helper method to get things DRY -->
<%= builder.label return_value_of_symbol(builder,:show_name), :class => 'control-label' %>
<div class="controls">
<%= builder.text_field :variable_value, :class => 'text_field' %>
<%= builder.hidden_field :variable_id, :class => 'text_field' %>
</div>
</div>
</p>
<% end %>
Note that we included the builder object(as specified in the fields_for call) in the parameters of our helper.
In our helper we define the return_value_of_symbol function
def return_value_of_symbol(obj,sym)
# And here is the magic, we need to call the object method of fields_for
# to obtain the reference of the object we are building for, then call the
# send function so we send a message with the actual value of the symbol
# and so we return that message to our view.
obj.object.send(sym)
end
Use label_tag, put the show_name on a instance variable on your controller and use like this:
<%= label_tag #show_name, nil, :class => 'control-label' %>
EDIT:
On your application_helper.rb, create a helper method similar to this one:
def show_name(name)
content_tag(:label, name, :class => 'control-label')
end
Then you can use the show_name(name) on your views like this:
<%= show_name(#name) %>
Just remember to populate the #name variable.
Related
I am looking for a more elegant way to accomplish this.
<div class="control-group">
<%= f.label :shoot_date, class: "control-label" %>
<div class="controls">
<% if #shoot.new_record? %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => Date.today.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% else %>
<%= f.text_field :shoot_date, :class => 'datepicker', :value => #shoot.shoot_date.strftime('%m/%d/%Y'), 'data-behavior' => 'datepicker', :readonly => true %>
<% end %>
<span class="help-block">Sitting date of this shoot.
</div>
Yeah, just throw it in the controller:
def new
shoot.shoot_date = Date.today
end
You shouldn't have to put it in the edit method or anything; that should happen automatically. You could even put it in the model if you like, but the controller works well.
I am trying to render a partial which I have set up as the following. I have am also trying to create a nested form whereby I have included accepts_nested_attributes_for :user in my hospital_bookings model. I seem to be getting the following error:
NameError in Rota_days#index
Showing
C:/Users/home/Desktop/Portal/app/views/rota_days/index.html.erb
where line #31 raised:
undefined local variable or method `hospital_booking' for
which is pointing to the following line <%= render :partial => "booking_dialog", :locals => { :booking => hospital_booking.new } %> of my index.html.erb as shown below. I thought it was something to do with my pluralization. By changing hospital_bookings.new to hospital_booking.new but this did not work
_booking_dialog.html.erb
<%= form_for booking do |f| %>
<%= f.fields_for :user do |f| %>
<br/>
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
hospital_booking.new is nonsensical: you have no local variable named hospital_booking. If you want a new instance of the HospitalBooking model, then you want HospitalBooking.new.
So:
<%= render :partial => "booking_dialog", :locals => { :booking => HospitalBooking.new } %>
Update (from the comments)
In the booking_dialog form partial, you need to put the name attribute on the associated user record inside a fields_for block, to distinguish it from the fields for the parent (booking):
<%= form_for booking do |f| %>
<%= fields_for :user do |user_fields| %>
<%= user_fields.label :name %>
<%= user_fields.text_field :name %>
<% end %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
p.s. it seems very strange that you have a hidden field for the :id in here. You shouldn't need that.
right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)
When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.
I have a partial that I use to show errors from an object onto a form.
<% if object.errors.any? %>
<div id="error_explanation">
<h2>Oops, looks like <%= pluralize(object.errors.count, "error") %>
occured:</h2>
<br />
<ul>
<% object.errors.each do |key, msg| %>
<li><%=key%><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
It works great for 1 model.
However I can't figure out how to make it work for a form that has two models.
Any ideas? I don't want to use the plugin I would like to have more control.
Just output the partial for each model in the form view with 2 models and pass actual model instances into this partial as local variable:
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model1} %>
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model2} %>