Specifying html attributes with form_for helper methods - ruby-on-rails

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" %>

Related

Hardcode partial input value in a Rails form_for field

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

no implicit conversion of Symbol into Integer in view forms

I am working on configuring the apartment gem for my rails app to give users the functionality to create subdomains. I have a nested form and when attempting to access "accounts/new" I am getting the following error:
no implicit conversion of Symbol into Integer in view forms
accounts/new.html.erb
<div>
</div>
<h2>Create an Account</h2>
<%= form_for #account do |f| %>
<%= f.fields_for :owner do |o| %>
<%= form_for o, :email do %>
<%= o.text_field :email, class: 'form-control' %>
<% end %>
<%= form_for o, :password do %>
<%= o.password_field :password, class: 'form-control' %>
<% end %>
<%= form_for o, :password_confirmation do %>
<%= o.password_field :password_confirmation, class: 'form-control' %>
<% end %>
<% end %>
<%= form_for f, :subdomain do %>
<div class="input-group">
<%= f.text_field :subdomain, class: 'form-control' %>
<span class="input-group-addon">.scrumteam.com</span>
</div>
<% end %>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>
</div>
accounts_controller.rb
private
def account_params
params.require(:account).permit(:subdomain, :owner_attributes => [:email, :password, :password_confirmation])
end
You are nesting multiple forms into each other which is not supported in HTML:
See this question for more details: Can you nest html forms?
These form_for lines look invalid in particular:
<%= form_for o, :field_name do %>
Here, o is a special FormBuilder object which should not be fed to form_for. Try this instead:
<%= f.fields_for :owner do |o| %>
<%= o.fields_for :email do %>
Unlike form_for, it is possible to nest fields_for blocks.
Not sure if this is your route problem, but form_for creates an actual form tag in html. You only need (and should have) 1, the fields_for allows you to switch the form helpers to a different object, but you don't need to call form_for again within it. If you're just trying to group your form fields, you can just add some divs and/or labels.

Adding a send email option in a form for a Rails model

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 %>

Get the name of a parent resource when working with a nested resource

I have a comment controller that uses a form partial to add comments. Now this controller is nested under any parent resource that needs to have comments.
resource :post do
resource :comments
end
resource :poll
resource :comments
end
If I want to have a form partial that automatically configured for the proper resource how would I do it?
Right now I have to set up forms on the page of the nested resource like so:
<%= form_for [#post, #comment] do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
I would like to have a partial that looks something like the above code but that I can just call <%= render 'comments/form' %>
Any ideas on the best way to make this happen?
You can solve this problem by passing a local variable to the comments partial using the locals hash:
In your nested resource, lets say, post's view:
<%= render 'comments/form', :locals => {:resource => #post} %>
Your comments form :
<%= form_for [resource, #comment] do |f| %>
<%= f.label :title %>
<%= f.text_field :title %> <%= f.label :body %>
<%= f.text_area :body %> <%= f.submit %>
<% end %>
Also, I encourage you to go through this guide on partials where the details are explained.

Fill rails form conditionally

I'm making a simplistic message board with tags. The message#index view displays a list of all messages. The tag#show view shows messages of a specified tag. On the message#index view, there is a form (partial) that requires the user to write a message and to tag it. On the tag#show view, I'd like to use the same form partial but to have the view's tag automatically filled into the form. In the show action of the tags controller, the name of the tag is #title.
The form partial looks like this:
<% form_for :message, :url => { :action => "create" }, :html => { :id => 'form' } do |f| %>
<%= f.error_messages %>
<%= f.label :tag, "tag<p2>( separate tags with a comma )</p2>" %>
<%= f.text_field :tag_list %>
<%= f.label :name, "name<p2>( optional )</p2>" %>
<%= f.text_field :name %>
<%= f.label :email, "email<p2>( optional )</p2>" %>
<%= f.text_field :email %>
<%= f.label :title, "message" %>
<%= f.text_area :content %>
<%= f.submit 'submit' %>
<% end %>
How do I auto fill the tag_list text field with the #title value? #title is a string. I appreciate any help you can offer. Thank you.
<%= f.text_field :tag_list, :value => #title %>

Resources