current action name problem - ruby-on-rails

When i am in /edit action, i see edit button but the problem is if there is an error in form validation it renders action edit and i see create button. how can i fix it?
<%= form_for(#page) do |f| %>
<% if controller.action_name =="edit" %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>

<% if ["edit", "update"].include? params[:action] %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>
Better solution is to extract your form as a partial and send local variable with button name to it
your edit view:
<%= render :partial => "form", :locals => { :button_label => "Edit" } %>
your create view:
<%= render :partial => "form", :locals => { :button_label => "create" } %>
your _form partial:
<%= form_for #object ... do |f| %>
...
<%= f.submit button_label %>
<% end %>
UPD
I think #idlefingers solution the best for your issue

You could just use f.submit with no arguments. This will create names like "Update Page" and "Create Page". If you want to change the wording of these, they can be set in your locale. No conditionals, no messing about with action names. Simple.

Try to do this check:
<%= form_for(#page) do |f| %>
<% if controller.action_name =~ /update|edit/ %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>

My solution:
<%= form_for(#page) do |f| %>
<%= f.submit(f.object.new_record? ? "Create" : "Update") -%>
<% end %>

Related

Render form partial from view ROR

Im trying to render a form from another controller in my view.
This is posts_index, and the render post.comments works fine, but the form for a new comment doesnt.
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render :partial => '/comments/form', locals: {post: post} %>
I get this error: undefined method `comments' for nil:NilClass
The comments form:
<%= form_for([#post, #post.comments.build]) do |f| %>
<%= f.label :Comment %><br />
<%= f.text_area :body, :class => "comment_text_box" %>
<%= f.submit %>
<% end %>
I understand I need to pass the post.comments to the form, but can't figure out how.
Post_show works with <%= render "comments/form" %>
Post and comments are a has_many relationship, posts has_many comments.
Thanks for looking.
You need to pass variables into your partial like this:
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render partial: '/comments/form', locals: {post: post} %>
<% end %>
and change your form partial to this:
<%= form_for([post, post.comments.build]) do |f| %>
// form fields
<% end %>
When you ask for the partial, you need to send it the post it's related to. It would look like this:
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render :partial => '/comments/form', post: post%>

Changing button value of a scaffold when in /new and /edit in rails

I generated a Stories scaffold and now when I'm in stories/new, the button that I click says "Create Story." How do I change the value of that to say something else, like "Create Tale"?
I've gone into the stories/new.html.erb and also the stories/edit.html.erb, but all that is there is
<%= render 'forms' %>
When I head to stories/_form.html.erb there is a
<div class="actions">
<%= f.submit class: "btn" %>
</div>
I know you can put <%= f.submit "Create Tale", class: "btn" %>, but if I do that it will say "Create Tale" for both creating and updating. How could I make is also say "Update Tale" for when I'm in stories/edit?
For giving it a name, try:
<%= f.submit class: "btn", name: "Create Tale" %>
For naming it differently depending on the action. What I normally do is pass the button-name into the form-template from the action-view.
Eg in new.html.erb:
<%= render partial: 'form', button_name: 'Create Widget' %>
in edit.html.erb:
<%= render partial: 'form', button_name: "Update Widget" %>
in form:
<%= f.submit name: button_name %>
The alternative is not to put the submit buttons in the "form" partial, but to keep them in the action-views eg for "new.html.erb"
<%= form_for #widget do |f| %>
<%= render partial: 'form' %>
<%= f.submit name: 'Create Widget' %>
<% end %>
for "edit.html.erb"
<%= form_for #widget do |f| %>
<%= render partial: 'form' %>
<%= f.submit name: 'Update Widget' %>
<% end %>
NOTE: code not tested and probably buggy, but you get the drift...
work directly on the form (partial _form)
<%= f.submit "Submit", class:"btn"%>
For Rails 5, change:
in new and edit views
and
the same way.
Then in form use:
instead of

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

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.

Ajax update doesn't function with Rails

I have a _follow_form partial :
<% unless current_user == #player %>
<div id="follow_form">
<% if current_user.following?(#player) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
My _follow partial looks like this :
<%= form_for current_user.relationships.build(:followed_id => #player.id),
:remote => true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
My create.js.erb file :
$("#follow_form").update("<%= escape_javascript(render('players/follow')) %>")
But nothing happens when I create a Relationship. I mean the relation is created, but the partial doesn't update.
When I try with :
$("#follow_form").append("foobar");
It works.
How could I update #follow_form ?
Thanks for your help.
PS: I searched for a solution before posting, but everything I tried failed.
UPDATE
With :
$("#follow_form").html("<%= escape_javascript(render('players/unfollow')).html_safe %>")
Nothing happens, the partial isn't replaced but an error occurs :
NoMethodError in Relationships#create
Showing C:/Ruby/ostriker/app/views/players/_unfollow.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
My _unfollow partial :
<%= form_for current_user.relationships.find_by_followed_id(#player),
:html => { :method => :delete },
:remote => true do |f| %>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
$("#follow_form").html("<%= escape_javascript(render('players/follow')).html_safe %>")

Resources