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 %>
Related
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])
I guess I'm a little confused on how fields_for works. I have an action with this:
3.times { #submitted_quiz.submitted_answers.build }
if I write a form_for like this in the associated view:
<%= form_for(#submitted_quiz) do |f| %>
<%= f.hidden_field :quiz_id, :value => #quiz.id %>
<%= f.hidden_field :name, :value => #quiz.name %>
<%= f.fields_for (:submitted_answers) do |ff| %>
<%= ff.label :content, "Answer" %><br />
<%= ff.text_area :content, :rows => 3 %>
<% end %>
<%= f.submit %>
<% end %>
how does fields_for know to run three times?
Rails will simply check how many submitted_answers your submitted_quiz has and generate appropriate number of fields ( one for each answer obviously ).
I'm trying to submit a form that contains fields for both an event and an invitation to that event. Here is my form:
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :start_at %>
<%= f.input :end_at %>
<%= f.input :all_day %>
<%= f.hidden_field :owner_id, value: current_user.id %>
<% if false %>
<%= f.association :sitter, label_method: lambda { |s| "#{s.name}" }, collection: User.all %>
<%= f.association :group, label_method: lambda { |g| "#{g.group_name}" }, collection: Group.where(:owner_id => current_user.id) %>
<% end %>
<%= f.input :user_emails, as: :text %>
<%= form_tag event_invitations_path, :method => :post do %>_tag :user_emails %>
</div><div>
<%= label_tag "Your message:" %>
</div><div>
<%= text_area_tag :email_message %>
<% end %>
</div>
</br>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
This is (perhaps obviously) not working. :user_emails is NOT part of the event or invitation model as it's a list of emails that will be used to create invitations. Basically I merged two forms, one that was accepting the email/send invitation piece and one that was accepting the event information. I think I have my controller and model set up properly to take care of this but how do i submit this information as part of the same form without getting an "undefined_method" error (since user_emails doesn't belong to events). Let me know if you want to see my model/controller.
Use FormTagHelper
<%= label_tag "User", "Email"%>
<%= text_area_tag "user_emails", "example#example.com"%>
See the documentation for more options on text_area_tag and label_tag
I've got this tripbuilder which i want to assign categories to. So I've set up the models as where a trip can have any(or more) categories that are in the category table in my database. However; i have no idea how i can set up the form allowing a user to select categories via checkbox. Since fields_for doesn't sound like a solid way to go in this case (Because i want to see all the categories with a checkbox and select as many categories as i want). Can anyone help me out?
I've tried this form:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<%= a.fields_for :categories do |cat| %>
<%= cat.check_box :name %>
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
At first, you need to setup the relationship between trip and category like this:
class Trip < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Then you can build the form like this:
<%= form_for #trip, :html => {:multipart => true} do |a| %>
<%= a.label :title, "Routetitel" %>
<%= a.text_field :title %>
<%= a.label :description, "Omschrijving" %>
<%= a.text_area :description %>
<% Category.all.each do |cat| %>
<%= check_box_tag "trip[category_ids][]", cat.id, #trip.catergory_ids.include?(cat.id)
<% end %>
<%= a.submit 'Verstuur' %>
<% end %>
Yes, it can be done by using select tag and multiple attribute of select tag.
<% = a.select :categories, Category.all.collect {|c| [c.name, c.id]}, :include_blank => true', :multiple => "multiple" %>
Please Modify your fields_for as described below and check !!!!
<%= a.fields_for "categories[]" do |cat| %>
<%= cat.check_box :name %>
<% end %>
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 %>