Rails4 : Referencing associated model in fields_for partial - ruby-on-rails

I broke out my form fields for a associated model into a partial. I want to reference the associated model in the partial but I'm not sure how to do that. #position and #position_fields are both nil. I see render ..., object: #something but I am not sure how to reference the current position model to pass it in.
apps/views/events/_form.html.erb
<%= f.fields_for :positions do |builder| %>
<%= render 'position_fields', f: builder %>
<% end %>
apps/views/events/_position_fields.html.erb
Fields for <%= #position.name %>
<%= f.text_field :name %>
How do I reference the associated model in the fields_for partial?

In your apps/views/events/_position_fields.html.erb, you can do the following:
Fields for <%= f.object.name %>
<%= f.text_field :name %>
This is because the builder has a reference to each of the position object and that you are passing builder as a local to the position_fields partial with the name f.
Reading through this doc will be useful: http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for.

Related

Get data attribute value from the Rails form builder without using input field

I have what I hope to be a simple question. I need to display the value for an attribute on the Edit page, while keeping the input field for the same attribute. How might this be accomplished?
Well generally you can just use the original object, like you'll have an #foo that you'll have used in your form_for statement, so you can just use that directly: = #foo.the_attribute
If you're within a partial, or elsewhere where you have only the form builder instance, then you can refer to the underlying object with the .object method, eg.:
= form_for #foo do |f|
# in here, f.object == #foo
In my case, I'm working with accepts_nested_attributes_for in two models. Event accept nested objects from Speaker. And Speaker has a perfil_id attribute which could be ['Maker', 'Developer', 'Entrepreneur', ...].
The Speaker's form is a partial rendered from the principal form, Event's form:
<%= form_for(event) do |f| %>
<%= f.text_field :title %>
<%= f.label :title, 'Event name' %>
<%= f.fields_for :speakers do |builder| %>
<%= render 'events/partials/speaker_fields', f: builder %>
<% end %>
<%= f.submit %>
<% end %>
Partial
<%= builder.number_field :name %>
<%= builder.label :name %>
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.member_profile_id ) %>
<%= select_tag "event[speakers_attributes][profile_id]", options, prompt: 'Select a Profile' %>
When editing Event's Speakers I wanted a select_tag to select the profile name for the actual Speaker.
I could not use an input field for this. So I need to get the correct values from the builder object and I get what I need by doing this:
f.object.profile_id
Passing it as a fourth param to the select options I get this working:
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.profile_id ) %>
I hope it could be useful for you too!

Non-ActiveRecord nested model form not taking model values

I'm using Rails' form_for and fields_for to get a form for a nested json attribute the model has, but I'm not getting the value when editing and have to set the value manually:
<%= f.fields_for :json do |ff| %>
<%= ff.text_field :attr, value: #app_configuration.json.attr %>
<% end%>
In order to get my json attr to be readable I do this on my controller:
#app_configuration.json = OpenStruct.new(JSON.parse(#app_configuration.json))
Why do I have to manually assing the value?
How does ff.text_field look for the attribute value?
You need to be using the builder variable ff declared using fields_for.
<%= f.fields_for :json, #app_configuration.json do |ff| %>
<%= ff.text_field :attr %>
<% end%>
Using f.text_field :attr will look for attr in the parent model which does not exist and the reason why you don't see values during edits.
For non AR, custom objects you also need to pass in the second parameter to fields_for as shown. Please see documentation on fields_for.

Is it possible to access a controller's instance variable in a partial if the partial is being rendered by a different controller's view?

Hi guys thanks in advance for the help. I am building a sort of online game using rails. The user interface is all supposed to be centered around one page: users/... I am running into an issue in creating and interacting with objects outside of users. For example, within the user page users are supposed to have the ability to create a fortress. The fortress of course is its own object with its own model and controller. If I were to try to put a form to create a fort using the form_for tag in the users page, the form wouldn't be able to access the fort controller's #fort instance variable and would throw an error. In my attempt to solve the problem I have created a partial view for forts called _new.html.erb containing the fort form
Here it is:
<h1>Create a New Fort</h1>
<%= form_for(#fort) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :ownership %>
<%= f.text_field :ownership %>
<div style="display:none;">
<%= f.label :xco %>
<%= f.text_field :xco %>
<%= f.label :yco %>
<%= f.text_field :yco %>
<%= f.label :territory %>
<%= f.password_field :territory %>
</div>
<%= f.submit "Create a Fort" %>
<% end %>
I then render that partial in my user show.html.erb here:
<%= render "forts/new" %>
Of course moving the form to a partial does nothing at all, but what I was hoping to do was something along the lines of this:
<%= render partial: "new", object: #fort %>
I would like to pass the fort instance variable into the partial then render the partial in the users view. I recognize however that the variable passed to the partial as described above comes directly from the view the partial is rendered in, and at that realization I am at a loss for ideas.
I have considered the key might be somewhere in associations and I have been crawling the internet for info on that. My models are set up so a user has_many forts and a fort belongs_to a user. I don't know whether or not that will help.
All help is appreciated,
Thanks loads,
Alex P
From your outer view, assuming #fort has been instantiated in your controller, you can pass #fort directly to your partial like this:
<%= render 'forts/new', :fort => #fort %>
I think what you are looking for is accepts_nested_attributes_for. This RailsCast explains how it is used.
In your case you would add accepts_nested_attributes_for :forts to your user model. I'm not sure what your user/show view looks like, but you should just be able to add a <%= f.fields_for :forts do |f| %> which would contain the fields currently in forts/new.
Yes, you can pass Locals with render statements wherever you call partial.
<%= render 'shared/error_messages', :locals => {:info => first, :img_style => "original"} %>

How do I create a nested resource?

I have a one to one relationship with Shares and Testimonials.
In Testimonial.rb:
belongs_to :share
In Share.rb:
has_one :testimonial
I want to add a form to the Shares page where I can create a testimonial which belongs to that specific share.
Inside the SharesController I set:
#testimonial = #share.build_testimonial
In the Shares view I have:
<%= form_for #testimonial do |f| %>
<%= f.text_area :message %>
<%= f.submit "Submit testimonial" %>
Is the above correct or do I have to add the share object to the view somehow?
What do I add to the create action in the Testimonials controller to create the testimonial and associate it with the #share object?
I have tried to send the share_id to the Testimonials controller from the view as an additional param and then used a "before" filter to find the share object but I don't think that's the right way to do it.
In the share.rb model, you'd want to add:
accepts_nested_attributes_for :testimonial
Since you are in the share view, I would think you'd have a construct in that view of:
<%= form_for #share do |f| %>
...
<% end %>
In that context, to use the testimonial fields:
<%= form_for #share do |f| %>
<%= fields_for #share.testimonial do |t| %>
<%= t.text_area :message %>
<% end %>
<% end %>

Rails many-to-many fields_for: How to access fields_for values?

I'm trying to created a set of nested (many-to-many) forms in rails 3. Everything works fine thanks to fields_for, but I need to put a title above each nested form. That title has the value of the profession_type.name field (which has a prepopulated value) in each respective nested form.
I'm having a heckuva time extracting that profession_type.name value from fields_for form objects. What I need to know is either:
a.) Is it possible to extract prepopulated values from fields_for objects, and if so how?
or
b.) How can I make a many-to-many nested form loop over only the relevant "fields_for" entry for each |specform| value, instead of all of them?
Any suggestions using either method (or alternative approaches) very much appreciated.
<% #professional.professional_specialties.each do |specform| %>
<%= specform.profession_type.name %>
<% f.fields_for :professional_specialties do |specialtyform| %>
<%= specialtyform.label :profession_type %>
<%= specialtyform.text_field :profession_type %>
<%= specialtyform.label :qualifications %>
<%= specialtyform.text_field :qualifications %>
<%= specialtyform.label :license_number %>
<%= specialtyform.text_field :license_number %>
<%= specialtyform.label :enabled %>
<%= specialtyform.check_box :enabled %>
<% end %>
<% end %>
The FormBuilder has attribute accessors like :object_name and :object.
For your particular issue try using:
<%= specform.object.name %>

Resources