I have a Rails form where the user is able to make a Top5 list. While they are able to label the list, all lists start with the phrase "Top 5 Greatest." Of course, it's easy to use Regex and append this phrase to the front of a list in the controller, but then I'm stuck doing it for both the create and update actions, so it's not very dry. Is there a way to hardcode a partial value in a rails form_for that will then be combined with the rest of the form input and sent to the controller?
This is currently what the form looks like (minus the form_for partial):
<br><%= f.label "Top Five Greatest:" %>
<%= f.text_field :title %><br>
<%= f.hidden_field :user_id, value: user.id %>
<br><%= f.label "#1" %>
<%= f.text_field :number1 %>
<br><%= f.label "#2" %>
<%= f.text_field :number2 %>
<br><%= f.label "#3" %>
<%= f.text_field :number3 %>
I'd really appreciate any insight.
Override setter method:
# put this in your model:
def title=(value)
super("Top 5 Greatest #{value}")
end
Related
I am trying to pull all of the values of <input> fields in a form_for and populate a string with them. I would not like to do form_for #instance_variable as I would not like to submit the params as the initialization of an Object.
In case you are wondering why I want to do this seemingly backwards thing, I am trying to have a form within a view, grab all the input values, put it into a string, and that use that string for the body of an email that would be delivered via a Mailer.
My recomendation would be something like this:
<%= form_tag foobar_path do %>
<p>
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</p>
<p>
<%= label_tag :another_field %><br />
<%= text_field_tag :another_field, params[:another_field] %>
</p>
<%= submit_tag "Submit"%>
<% end %>
Then in your controller do something like this:
def foobar
Mailer.send_something(params[:email],params[:another_field]).deliver
end
Is there a reason why this will not work and you really need to make an instance of an object?
http://guides.rubyonrails.org/getting_started.html#installing-rails
In this tutorial. Can anyone tell me how is the form getting generated.
<%= form_for :article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Where is the :article hash coming from?.
f.label corresponds to the form object, where is the :title coming from?.
Maybe this guide can answer your question:
RailsGuides - Form Helpers
Take a look the "2 Dealing with Model Objects" session
You are calling form_for with the symbol :article which means that the form creates a new instance of Article for use in the form.
I've not tested this code but I would imagine :title is an attribute of Article, either a field in the database or a virtual attribute ect.
Hi if you would just keep following along the tutorial in section 5.4 the root for this elements is explained.
After my users fill out a form, I want them to have the option of sending or not sending an email.
I can do this easily in the controller by doing something like:
send_email if params[:entry]
but I'm not sure how to introduce this param under my form_for, since it isn't part of the model.
How can I get this param to show up in the view and be available upon submit?
Use the #check_box_tag form helper in the form_for block
It can be something as simple as this:
<%= form_for #notice do |f| %>
<%= f.label :text, 'Notice Text' %>
<%= f.text_area :text %><br />
<%= label_tag 'entry', 'Send Email?' %>
<%= check_box_tag 'entry' %>
<%= f.submit %>
<% end %>
I have a form, that among other things, contains about 20 different checkboxes. Like so:
<%= form_for #inventory do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
...
<p>
<%= f.check_box :apple %><%= f.label :apple %><br />
<%= f.check_box :banana %><%= f.label :banana %><br />
<%= f.check_box :orange %><%= f.label :orange %>
...
</p>
...
<% end %>
What I want to do is take the value of the selected checkbox, comma delimit them, and save them in a column in the db. So if the apple and orange checkbox is checked it saves as:
#inventory.fruit = "apple, orange"
how do I do this?
I don't think we can send multiple values as a string rather than an array. Look at the below solution
In Rails, how to handle multiple checked checkboxes, just split on the , or?
The solution is in pure HTML code but you can use check_box_tag instead.
Is it possible to specify html attributes while using the form_for helper methods?
For example:
<% form_for #user do |f| %>
<%= f.label :username%>
<%= f.text_field :username %>
<%= f.submit "Signn Up" %>
<% end %>
How would I go about specifying the class for the label? Is it possible, or do I have to resort to label()?
On mostly helpers, the last arg is a hash of html options for the element.
<%= f.label :username, "Username", :class => "class" %>