passing variable to simple form symbol - ruby-on-rails

this is my _form_item partial in which I used symbol :order_item
<%= simple_form_for :order_item do |f| %>
.....
<% end %>
here is my view in which I want to render that partial:
<%= content_tag_for :tr , #order.order_items do |i| %>
<div class="hide">
<%= render :partial => "form_item" %>
</div>
<% end %>
How can I pass "i" object to :order_item?
UPDATE:
I prefer to keep it ":order_item" instead of changing it to something like "foo".

You should have:
<%= render partial: 'form_item', locals: {order_item: i} %>
or a shorthand:
<%= render 'form_item', order_item: i %>
And in your form_item partial you should have:
<%= simple_form_for order_item do |f| %>
...

Just do
<%= simple_form_for item do |f| %>
.
.
<% end %>
and render the partial via
<%= render :partial => 'form_item', :locals => { :item => i } %>

You can pass i as a locals to the partial like follows:
<%= render :partial => "form_item", :i => i %>
and you'll have i available in the _form_item partial.
Update:
Local variables passed to partials are variables and not symbols. You could keep the name order_item like follows:
<%= render :partial => "form_item", :order_item => i %>
And update your order_item partial as follows:
<%= simple_form_for order_item do |f| %>
.....
<% end %>
I think I understand why you do not want to change :order_item to just order_item. I think you are going to have to update your other calls to pass in local variable order_item where ever you are making a call to this partial.

Related

Ruby On Rails - Simple_fields_for with nested_form, pass variable

Is it possible to pass variable when using nested form with Simple form?
Like
<%= simple_nested_form_for(#foo) do |f| %>
...
<%= f.simple_fields_for :bar %>
<%= f.link_to_add :bar do %>
Add bar
<% end %>
<% end %>
I have tried
<%= f.simple_fields_for :bar, :locals => {:baz => 'baz'} %>
but it wont pick it up in the partial.
Partial: _bar.html.erb
<%= baz %>
Simple Form Gem
Nested Form Gem
I was looking for the same thing and never did see an answer anywhere, so I'm posting an example of what worked for me:
<%= f.simple_fields_for :answers do |answers| %>
<%= render 'answer_fields', { f: answers, question: #question } %>
<% end %>
Also, if you want to access the current object from the collection being rendered within the partial, use f.object within the partial.
To pass variables to partials, you use the :locals option:
<%= render partial: "my_awesome_partial", locals: {variable: 5, baz: 'baz'} %>

adding an if statement within brakets in ruby on rails

I have the following in my view .html.erb:
<%= form_tag :action=>"edit", :id => #product.id do %>
I want to basically only add the id if #product exists (not null i suppose) so i want to do something along the lines of:
<%= form_tag :action=>"edit" if #product print",:id => #product.id" end do %>
I know the above is wrong code but that's the idea behind it, not sure if i can embed that within the <%= tag.
Thanks
try
<%= form_tag :action=>"edit", :id => (#product.id if #product) do %>
<%
url_options = { :action => "edit" }
html_options = {}
html_options[:id] = #product.id if #product
%>
<%= form_tag url_options, html_options do %>
<% .... %>
<% end %>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
http://apidock.com/rails/v3.2.8/ActionView/Helpers/FormTagHelper/form_tag
This should work:
<% if #product %>
<%= form_tag :action=>"edit",:id => #product.id" do %>
<% else %>
<%= form_tag :action=>"edit" do %>
<% end %>
You can probably embed the conditional code in the <%= ... %> tag if you use eval but the above might be more clear!

passing variable into partial

I have the following for and partial setup but i keep on getting an error, that the partial does not recognise the variable |builder|.
Form
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render 'event_fields', :builder => builder %>
<% end %>
<%= end %>
_events_fields partial
<fieldset>
<%= builder.input :name, :collection => ['Applications Open', 'Applications Close', 'Traineeship Starts'] %>
<%= builder.text_field :start_at, :class => 'datepicker' %>
<%= builder.hidden_field :all_day, :value => true %>
<%= link_to "remove", '#', class: "remove_fields" %>
any idea how i should be padding the variable across? or if in fact this is the correct way of doing it? It would be really helpful is someone could help me understand a little more about how and why you need to do this.
You need to tell it that it is a partial view and pass in a hash to the locals option. Like so:
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render partial: 'event_fields', locals: {builder: builder} %>
<% end %>
<% end %>
If you're using Ruby 1.8 then:
<%= simple_form_for #firm do |f| %>
<%= f.fields_for :events do |builder| %>
<%= render :partial => 'event_fields', :locals => { :builder => builder } %>
<% end %>
<% end %>
partial_name would be replace with actual partial name or partial name with directory name.
We would provide the data_object object to the partial, available under the local variable data_variable in the partial and is equivalent to:
<%= render partial: "partial_name", locals: { data_variable: data_object } %>

Rendering form partials from other controllers

I have a few shared partials that I can render from any controller fine however I am having a bit of trouble rendering form partials from another controller. I am wanting to be able to add notes to my contacts
In my contacts/show.html.erb i have the following
<% render :partial => "notes/form", :note => Note.new %>
In my notes/_form.html.erb i have the following
<%= form_for #note do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<p>
<%= f.label :content %><br />
<%= f.text_field :content %>
</p>
<p>
<%= f.label :contact_id %><br />
<%= f.number_field :contact_id %>
</p>
<p><%= f.submit %></p>
<% end %>
However I get the error:
Showing /Applications/Rails/apps/saas31/app/views/notes/_form.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for #note do |f| %>
2: <%= render 'shared/error_messages', :object => f.object %>
I'm starting to get the hang of rails but having a few small frustrating problems as to be expected when learning anything new i suppose. Anyone have any ideas?
Your local variables should be passed through in a locals hash.
<% render :partial => "notes/form", :locals => {:note => Note.new} %>
Read section 3.4.4 here.
Your partial also shouldn't use instance variables, change the following:
<%= form_for #note do |f| %>
to:
<%= form_for note do |f| %>
edit
If you want to use an instance variable, you can do the following:
<% render :partial => "notes/form", :locals => {:note => #note} %>
Ran into the same issue and this post was helpful in solving. Adding my notes and hopefully it will help someone else :)
I had a a Users controller and a _form.html.erb that rendered fine when I would access the /users/new page. I was trying to render the form as a partial from my /layouts/application.html.erb, as I wanted to give users the ability to create a new user from any page.
I ended up creating a new method (new_user) in application_helper.rb. Here is the code:
def new_user
User.new
end
I then render the partial from application.html.erb with:
<%= render :partial => 'users/form', :locals => {:user => new_user} %>

Pass a variable into a partial, rails 3?

I have a loop like such:
<% #posts.each do |post| %>
<% render middle %>
<% end %>
Then in my middle partial, how do I access the current post?
Try this:
<% #posts.each do |post| %>
<%= render 'middle', :post => post %>
<% end %>
Like this you'll have a local variable post available within the partial.
Give it to the partial as a local variable
<%= render :partial => 'middle', :locals => { :post => post } %>
Of course, rails also has a shortcut for rendering collections:
<%= render :partial => 'post', :collection => #posts %>
In this case it will call the partial post for every post with a local variable 'post'
You can even render a spacer template between each post:
<%= render :partial => 'post', :collection => #posts, :spacer_template => 'post_divider' %>
<% #posts.each do |post| %>
<% render middle, :post => post %>
<% end %>
You can now access post as the local variable post in the partial
Replace <%= render middle %> with <%= render middle, :post => post %>. Then in your middle partial, you can access the post variable.
You can replace the entire each block with this:
<%= render partial: "product", collection: #posts %>
Or even shorter:
<%= render #posts %>
Full documentation (section 3.2)
https://guides.rubyonrails.org/action_view_overview.html

Resources