adding an if statement within brakets in ruby on rails - 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!

Related

rails form trying to get the posts path

I cant get the syntax of this correct I'm trying to include :url => posts_path in the form_for section
<% form_for :post do |f| %>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %></p>
<p><%= f.submit %></p>
<% end %>
form_for expects an instance of a model, not a symbol:
<% form_for Post.new do |f| %>
...
<% end %>
There are a few ways to do this. Like #meager, you want to call Post.new. I usually do it in the controller, though.
posts_controller.rb
#post = Post.new
view
<% form_for #post do |f| %>
...
<% end %>
You can use a custom path if that's where you want to send the data. So if you have a special case, you can make a route for it
routes.rb
get '/special', to: 'posts#special'
then you can have a form that says...
<% form_tag special_path do |f| %>
...
<% end %>
and the params will pass as you'd think. Notice the form_tag instead of form_for though.

Rails how can I pass multiple values through check_box_tag?

I am trying to pass multiple values to a custom function that I created through the check_box_tag, however I don't really know how to do it, I have check online for hours but didn't help.
Basically I have a details view, and I try to pass the date and id information of the detail to the controller and call the create method.
<%= form_tag( { :action => 'create' } ) do %>
<% #details.each do |detail| %>
<%= check_box_tag 'date[]', detail.date, false, :id => detail.id %>
<%= detail.date %>
<% end %>
<%= submit_tag 'Register!' %>
<% end %>
I try to set the custom value but when I type params in the debugger this is what it shows
{"utf8"=>"✓", "authenticity_token"=>"3PKBBKNmXyAfdSllTWBFP8EafhbrJ8rCgOeOp2NbeBA=", "date"=>["2013-06-08"], "commit"=>"Register!", "action"=>"create", "controller"=>"line_items"}
I really don't know how should I do it.
Thank you for your answer in advance!
please using array dates.
<%= form_tag( { :action => 'create' } ) do %>
<% #details.each do |detail| %>
<%= check_box_tag 'detail[dates][]', detail.date, false, :id => detail.id %>
<%= detail.date %>
<% end %>
<%= submit_tag 'Register!' %>
<% end %>

passing variable to simple form symbol

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.

Refactoring into partials in Rails

In page1, I use code A+B+C
In page2, I use code B+C
So when I make a partial, I realy have no idea in how to deal with this.
For example, In a Post-Comment system. I want to show #comments in 2 different pages. In the comment index page,
We show the post it belongs to. And in the post show page, We only have to show the comments content.(Since there is no need to show the comment.post again)
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= comment.author %>
<%= comment.content %>
<% end %>
..
#Post Show Page
<% #comments.each do |comment| %>
<%= comment.author %>
<%= comment.content %>
<% end %>
So, how do I make a partial to reuse the code? Perhaps like this? But this there more elegant way of doing this?
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= render comment %>
<% end %>
#Post Show Page
<% #comments.each do |comment| %>
<%= render comment %>
<% end %>
Updated:
I adopt the local variable approach, and update my code like:
# partial
<% if include_topic %>
<div class="Topic">
<h5><%= link_to "#{comment.topic.content}", comment.topic %></h5>
</div>
<% end %>
#Index
<%= render #comments, :locals => {:include_topic => true } %>
But I get undefined local variable or method `include_topic' for #<#
I just find nowhere to debug this issue
Your partial:
<%= comment.post if include_post %>
<%= comment.author %>
<%= comment.content %>
your code:
#index page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => true } %>
#show page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => false } %>
Syntax could be much shorter but it depends whether or not you stick to rails conventions see doc.
Sidenote: I don't like 1.8.7 syntax
In the partial,
<% comments.each do |comment| %>
<%= comment.post if params[:controller] == "comments" %>
<%= comment.author %>
<%= comment.content %>
<% end %>
and now render this partial in both comments/index and posts/show pages by specifying the comments as a local variable.

link submit to another page

I have this form:
in header of my website:
<% form_tag request.path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
controller:
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
#users1 = User.simple_search(params[:query]).all
end
model:
acts_as_simply_searchable :columns => [:name, :email]
view
<%= will_paginate %>
<ul class="users">
<%= render #users1 %>
</ul>
<%= will_paginate %>
displays a user
I want to link the submit button to index.html.erb (i have assigned a path in routes.rb). So that the user can look at the search results
You don't do this at the submit button, but at the form_tag URL (the first parameter). It would be something like this:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
Simply change request.path to the desired path.
I think you are missing the '=' sign and string interpolating the 'request.path'
This Worked for me:
<%= form_tag "#{request.path}", method: "get" %>
...the rest of your code ...
<% end %>

Resources