Rails pass radio button params - ruby-on-rails

Hey guys I am a newbie so go easy on me.
I am trying to figure out how to pass to the controller the radio button choice. It is NOT part of the model. Here is the code:
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= link_to 'Delete', payment_path(:id => #payment.id), method: :delete, data: { confirm: 'Are you sure?' } %></td></h4>
When I click delete all here is what the params look like:
Parameters: "authenticity_token"=>"lfFsfN04ajhIpYI13bwhmGtLtQIdRUkaClPsBBP12SSZwDEnsIslOqH3yptGvWUF620bEYhPBgbD7sLYV2cA7Q==", "id"=>"175"}
Thank for the help.

form_for can handle that easily.
<%= form_for #payment, method: :delete do |f| %>
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= f.submit 'Delete', data: { confirm: 'Are you sure?' } %></td></h4>
<% end %>
The post parameters received by the server:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KoCb84m88ptGUPbY9eofUMjpGLGmpKfUwBxB2t+Fv+QABr6lWnWpsEuJQ/quOJN3g6Zm+DZy2JTdVokjMQxLvQ==", "remove"=>"yes", "commit"=>"Delete", "id"=>"1"}

Related

Rails delete link url took wrong page

If I delete a post, it takes me to post/index.html.erb, but I want it to redirect to the profile/index.html.erb page.
<% current_user.posts.each do |post| %>
<%= post.post_name %> </p>
<%= link_to "view", post, class: "btn btn-default" %>
<%= link_to "Delete", post_path(post), method: :delete,
data: { confirm: "Are you sure?" },
class: "btn btn-default" %>
<% end %>
You're looking at the wrong code. Your view has nothing to do with this. Fix/change the redirection in your PostsController#destroy.

Trying to adapt code form one of my views to be rendered in a partial

I have a partial that takes a column from a database a iterates over it and displays all the urls on the web page. I want to add the lines that I have written in my app/views/topics/show.html.erb page that allow the user to edit, delete, or like the link.
These are the three lines I want to adapt to be added to the partial, the first two are buttons to carry out the edit and delete function and the third is a link to a partial with the like code.
<%= link_to "Edit bookmark", edit_topic_bookmark_path(bookmark.topic, bookmark), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [bookmark.topic, bookmark], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %>
<%= render partial: 'likes/likes', locals: { bookmark: bookmark } %>
this is the partial I would like to adapt it to located in app/views/bookmarks/_bookmarksandlikes.html.erb
<div>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%>
<%y=y+1%>
<% end %>
</h3>
</div>
and it is being called from app/views/users/show.html.erb with these two lines.
<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: #bookmarks} %>
<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: #liked_bookmarks} %>
here is how I have tried to insert the code into the partial
<div>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%>
<%y=y+1%>
<%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %>
<%= render partial: 'likes/likes', locals: { x: x } %>
<%end%>
</h3>
when I run the code as is I get an error page that says "undefined local variable or method 'bookmark' for #<#<Class:0x007fdfb1b39c80>:0x007fdfb1988d50> Did you mean? bookmark_url or #bookmarks
and says there is a problem on line 3 of the partial with the code for liking a page in app/views/likes/_likes.html.erb
<% if policy(Like.new).create? %>
<div>
<% if like = current_user.liked(bookmark) %>
<%= link_to [bookmark, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"></i> Unlike
<% end %>
<% else %>
<%= link_to [bookmark, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"></i> Like
<% end %>
<% end %>
</div>
<% end %>
side note at this point in working through this problem, it appears that only the third line for the partial I am trying to insert is giving me problems, the edit and delete buttons are rendering nicely.
Even if you don't know the answer I am quite keen to hear any thoughts on what I am not considering in this question or just your thoughts, also if you need more information please just let me know and I will post more.
Thanks for looking at my question.
I figured out my problem, I could not use the partial because the partial app/views/likes/_likes.html.erb was taking in a different variable than was being passed by app/views/bookmarks/_bookmarksandlikes.html.erb so I just put the needed code into app/views/bookmarks/_bookmarksandlikes.html.erb and adapted it like so.
<div class=row>
<h3>
<%y=1%>
<% mark.each do |x| %>
<%=y%>)<%= link_to x.url, x.url%><br>
<%y=y+1%>
<%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br>
<%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %><br>
<% if like = current_user.liked(x) %>
<%= link_to [x, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"></i> Unlike
<% end %>
<% else %>
<%= link_to [x, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"></i> Like
<% end %>
<br>
<% end %>
<%end%>
</h3>
</div>

Rails Partial: Undefined Local Variable

I use rails infrequently, so pardon if this is obvious. I've looked at the other questions, am working from Ruby On Rail Guide, and it looks like I'm doing this correctly, but I still can't get my local variables to work.
todays/show.html.erb
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: #today_lines %>
</p>
next_steps/_next_step_today.html.erb
<p>
<%= render partial: "next_steps/next_step",
locals: {next_step: today_line.next_step} %>
<%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
next_steps/_next_step.html.erb
<span class="<%= next_step.complete ? "completed_next_step" : ""%> next_step_span_<%= next_step.id%>">
<%= form_for(next_step,
:remote => true,
:html => {:'data-type' => 'json',
:multipart => true,
:id => "edit_next_step_#{next_step.id}_#{rand()}"}
) do |f| %>
<%= f.hidden_field( :id, :value => next_step.id) %>
<%= f.hidden_field( :note_id, :value => next_step.note_id) %>
<%= f.hidden_field( :content, :value => next_step.content) %>
<%= f.hidden_field( :due_date, :value => next_step.due_date) %>
<%= f.check_box( :complete, :class => "next_step_complete_button" ) %>
<%= next_step.content %>
<% end %>
</span>
I know that the last partial _next_step.html.erb was working before I starting trying to add partials for extra flexibility. Now I can't seem to get the locals passed in correctly.
My previous working code was
todays/show.html.erb
<p>
<strong>Tasks:</strong>
<% #today_lines.each do |today_line| %>
<p><%= render today_line.next_step %></p>
<% end %>
</p>
Error message
NameError in Todays#show
Showing /.../app/views/next_steps/_next_step_today.html.erb where line #3 raised:
undefined local variable or method `today_line' for #<#:0x007f930adbd860>
Extracted source (around line #3):
<p>
<%= render partial: "next_steps/next_step",
locals: {next_step: today_line.next_step} %>
<%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
Any ideas?
EDIT: FINAL WORKING CODE
todays/show.html.erb
<p>
<strong>Tasks:</strong>
<%= render partial: "next_steps/next_step_today",
collection: #today_lines %>
</p>
_next_step_today.html.erb
<p>
<%= render "next_steps/next_step",
next_step: next_step_today.next_step %>
<%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
collection: does require that you use the partial file name for the local var.
I also found in my searching that if you don't use partial: you can also omit locals: and just use the variable name instead, which is cleaner. This change is shown in _next_step_today.html.erb. I wish I could also name the collection variable, instead of using the partial file name, or instead of using an each, but this is close enough, and working.
The name of the variable within the partial is derived from the name of the partial itself, not from the name of the collection.
Your _next_step_today.html.erb should then be the following:
<p>
<%= render partial: "next_steps/next_step",
locals: {next_step: next_step_today.next_step} %>
<%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>
Edit: if you want to stick to the #collection convention, that is
Show needs to specify the key name that the partial will use. So:
<%= render partial: "next_steps/next_step_today",
today_lines: #today_lines %>
Fix the spelling in next_step_today partial as well:
<%= render partial: "next_steps/next_step",
locals: {next_step: today_lines.next_step} %>

Passing HTML from serverside to datatables

I am changing my Datatables to load content from my Rails app. In one of the cells I have a big chunk of HTML, and since when using the server-side approach the cells are printed with Javascript, I would need to pass the HTML from Rails.
So, here is how it looks one of the cells with datatables WITHOUT server side fetching:
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order.id), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= form_tag(admin_order_set_completed_status_path(order), :method => 'patch' ) do %>
<%= submit_tag 'Complete order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= form_tag(admin_order_process_order_path(order), :method => 'post' ) do %>
<%= submit_tag 'Process order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= form_tag(admin_order_path(order), :method => 'delete' ) do %>
<%= submit_tag 'Delete order', class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' } %>
<% end %>
</div>
<% end %>
</div>
</td>
Now, since all the data will come back from Rails, I would need to pass that HTML via a JSON. Isn't that breaking the MVC? How do you deal in these situations?
If you are wanting to use json with ajax for datatables you can use JBuilder or rabl to create a json document that can be easily reused.
Also there is not a need to write out form_tags wrapped around submit_tags to call different methods, with rails you can use link_to and specify the :method to accomplish the same thing.
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= link_to "Complete order", admin_order_set_completed_status_path(order), method: :patch, class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' }%>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= link_to "Process order", admin_order_process_order_path(order), method: :post, class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' }%>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= link_to "Delete order", admin_order_path(order), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' }%>
</div>
<% end %>
</div>
</td>

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Resources