Fill rails form conditionally - ruby-on-rails

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

Related

Saving checkbox value in erb - rails

I have a nested form that saves information to three different models. One section of the form uses checkboxes and is supposed to save values 1-5. However, even when the boxes are checked the form returns value 0. I have tried several different variations of code for setting the checked value. Any help would be much appreciated. A section of the form code is below:
<%= form_for #newinstructor do |f|%>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.fields_for :through_ats do |tag_field| %>
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
<%= label_tag(:tag, "Music") %>
<%= tag_field.check_box(:tag_id, :value => 3) %>
<%= label_tag(:tag, "Outdoors") %>
<%= tag_field.check_box(:tag_id, :value => 4) %>
<%= label_tag(:tag, "Food") %>
<%= tag_field.check_box(:tag_id, :value => 5) %>
<% end %>
<%= f.submit %>
<% end %>
Did you permit tag_id in params?
params.require(:instructor).permit(:name,
through_ats_attributes: [:id, :tag_id, :_destroy]
)
To fix the problem I had, I had to uniquely define each of the symbols in tag_field.checkbox, then require/permit them individually in params
so in the form i put:
<%= label_tag("Please check off the categories your activity fits into.") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:cooking) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:art) %>
instead of :
<%= label_tag("What categories does your activity fit into?") %><br>
<%= label_tag(:tag, "Cooking") %>
<%= tag_field.check_box(:tag_id, :value => 1) %>
<%= label_tag(:tag, "Art") %>
<%= tag_field.check_box(:tag_id, :value => 2) %>
and in the controller:
params.require(:instructor).permit(through_ats: [:cooking, :art, :music, :outdoors, :food])
instead of:
params.require(:instructor).permit(through_ats: [:id, :tag_id])

Nested Form in rails 4

I have two Model Product and Supplier.
Product has_one supplier.
how to Build a Seach form inside a form.
,I want to Search Supplier and Put that id into My Product form.
Currently my code look like this.
<%= f.text_field :product_code, class: 'form-control'%>
<%= f.text_field :product_name, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= render :partial => 'supplier_search' %>
<%= f.submit 'Save', :class=>'button add'%>
Render suplier_search partial after submit button of product form.
<%= f.text_field :code, class: 'form-control'%>
<%= f.text_field :supplier_id, class: 'form-control'%>
<%= f.submit 'Save', :class=>'button add'%>
<%= render :partial => 'supplier_search' %>
Or
Don't have form in suplier_search partial. Just put input field or whatever you want, there and submit those fields via Ajax and get your required results in product form.
if its not partialing for <%= render :partial => 'supplier_search' %>
then use
"your views folder name/supplier_search" %>

Converting form tag to simple form

How would I go about converting this form tag below into a form_for?
<%= form_tag(contact_email_path, :method => 'post') do %>
<%= label_tag "Your email" %>
<%= text_field_tag "sender", #sender, :autofocus => true %>
<%= label_tag "Subject" %>
<%= text_field_tag "subject", #subject %>
<%= label_tag "Message" %>
<%= text_area_tag "message", #message %>
<%= submit_tag "Send Email" %>
<% end %>
form_for is a helper for creating forms which create or edit a resource.
If you have a resource here that you would like to create in your database, you would use this method. What it looks like you're doing here is not creating a resource, but sending an email. If that is the case, then a form_tag is probably a better option.
If you are, however, trying to create a new resource in the database (i.e. an new instance of ContactEmail or some other class), then you could do it like this:
<%= form_for #contact_email do |f| %>
<%= f.label :sender, "Your email" %>
<%= f.text_field :sender, :autofocus => true %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :message %>
<%= f.text_area :message %>
<%= f.submit "Send Email" %>
<% end %>
This assumes that #contact_email is an object that has the methods sender, subject and message and that you have resources :contact_email in your routes file.

Rails 3.1 - Displaying form values on Edit?

I feel like this should be really really simple, but I'm completely stuck!
In a form I might have a field like:
<%= f.text_field :name, :class => "text" %>
On edit, this pulls back in the value submitted on create, that's fine. But I want to prevent certain fields from being edited. I know I can disable the field or hide it, but I'd like to be able to pull in the values to display and use in other ways. How do I access them?
In this case I've tried things like:
<%= f.track.name %>
<%= track.name %>
<%= #track.name %>
But none of the above work!
Any ideas folks?
EDIT: (I'm using nested forms for this)
<%= form_for(#release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
And the upload_track_fields that are rendered:
<%= f.text_field :position, :class => "text" %>
<%= f.text_field :name, :class => "text" %>
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
<%= f.hidden_field :primary_genre_id %>
<%= f.hidden_field :secondary_genre_id %>
<%= f.hidden_field :alt_primary_genre %>
<%= f.hidden_field :alt_secondary_genre %>
<%= f.hidden_field :asset_tier %>
<%= f.hidden_field :preview_start %>
<%= f.hidden_field :parental_advisory %>
<%= f.hidden_field :available_separately %>
<%= f.hidden_field :_destroy %>
I've hidden most of the fields to prevent editing, but still need to see some of the fields so they're left as text fields. I tried to disable them, but that stops any changes (specifically the file upload) working.
In short, i'd prefer to display most of the above as text rather than form fields.
In the main form:
<% index = 0 %>
<% f.fields_for :tracks do |builder| %>
<%= #release.tracks[index].name %>
<%= render 'upload_track_fields', :f => builder %>
<% index += 1 %>
<% end %>
In the nested form:
<%= f.text_field :position, :class => "text" %>
# Notice that there's no "name" attribute
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
What I did in the first snippet is dirty, but I never used fields_for so I don't know how to get the actual index. I looked in Google, but I didn't find a solution so far.
I can't try it right now, I'll do it when I'll be home.
I suggest using this while finding a way to get the index.
Good luck!
As those who have commented said, I'd assume the <%= #track.name %> should work, if you have #track = #release.track (for instance) in your edit method in the controller.
Instead of keeping track of the index you can access the associated objects through builder, it's actually a track
<% f.fields_for :tracks do |builder| %>
<%= builder.object.name %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>

Specifying html attributes with form_for helper methods

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

Resources