Rails 3 Add to Cart on Show Page - ruby-on-rails

I have the following on my index file
<%= button_to '', line_items_path(:product_id => product), :remote => 'true', :class => 'addbasketbtn' %>
However I can not add this to the Show page it give me an error.
Am I doing something wrong here ?

I guess in your controller you have
#product = Product.find(params[:id])
In your view just replace product with instance variable #product
<%= button_to '', line_items_path(:product_id => #product), :remote => 'true', :class => 'addbasketbtn' %>

Related

SimpleForm: SyntaxError in Controller

Any idea on what could be wrong in this form?
Error:
vehicleTrack.html.erb:141: syntax error, unexpected keyword_do_block, expecting => ... params[:rangefrom_string]} do |f| #output_buffer.safe_appe... ... ^
This is my view:
<%= simple_form_for '', url: convertTrackToArea_path, :method => :post,
{ :controller => "vehicles",
:action => "convertTrackToArea",
:search => params[:search],
:rangefrom_string => params[:rangefrom_string]} do |f| %>
<%= f.input :areano, :label => 'Areano' %>
<%= f.button :submit, value: "Crear",:name => nil%>
<% end %>
I have not used simple_form so my guess is you are passing the last last argument incorrectly and it wont take a hash as argument and it detects the argument as key and searching for a value so throws error that => is missing, which is used to identify value in hash. So you can do something like this I suppose:
<%= simple_form_for '',
:url => url_for(:action => 'convertTrackToArea', :controller => 'vehicles',:search => params[:search],
:rangefrom_string => params[:rangefrom_string]),
:method => 'post' do |f| %>
since you are specifying controller and action you dont need to mention convertTrackToArea_path . If that route is already setup, you can just use that like in your posted question and remove controller and action name like:
<%= simple_form_for '', :url => convertTrackToArea_path(:search => params[:search],:rangefrom_string => params[:rangefrom_string]),:method => 'post' do |f| %>

Bind destroy link for a newly created item with Ajax in Rails 4

I am implementing comments in Rails 4 application.
I want to create comment and delete comment to work remotely with Ajax.
It works fine except the delete link for a newly added comment doesn't work.
What is the best way of making delete link work for a newly created item?
/views/posts/ show.html.haml
.post
%h1= #post.name
%h2 Comments
= render 'comments/form', comment: #comment
%ul#comments
= render 'comments/comment', :collection => #post.comments, :as => :comment
/views/comments/_form.html.haml
.comment-form
= simple_form_for comment, :remote => true do |f|
= f.input :body, :input_html => { :rows => "2" }, :label => false
= f.button :submit, :class => "btn btn-primary"
/views/comments/_comment.html.haml
%li{:"data-id"=>comment.id}
=comment.body
%br
= link_to "Destroy", comment_path(comment), :confirm => "Are you sure?", :method => :delete, remote: true, :class=>'link_comment_delete'
/views/comments/destroy.js.rb
$('#comments li[data-id=<%=#comment.id%>]').hide();
/views/comments/create.js.rb
$("#comments").append("<li><%= escape_javascript render #comment %></li>");
// ??? what to write here to make Delete link work
I've had the same exact situation, and ended up re-thinking the delete view by using .closest instead of biding to data-id, so you can do something like:
$("a.link_comment_delete").on('ajax:complete',function(e) {
$(this).closest('div.comment.body [or whatever class / id you are using]').fadeOut(100)
});

Hidden field in rails form

I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%= f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
However, as #AntonGrigoriev pointed out, you should use a object if you have, like this
<%= form_for #task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
or you can simply use the hidden_field_tag
<%= hidden_field_tag :task_id, task.id %>
Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :
hidden_field_tag(name, value = nil, options = {}) public
eg:
<%= hidden_field_tag(:field_name,value=#offer_status)%>

In my rails erb, how can i replace an ajax button_to by a link_to?

I have the following button :
<%= button_to 'Add to Cart', line_items_path(:product_id => product),
:remote => true %>
I want to replace it by a link_to containing an image with text on it.
I am ok with the HTML CSS part, but i want the request to be for line_items#create not for line_items#index
How can i do that?
Try this:
<%= link_to "Add to Cart", {:controller => "line_items", :action => :create}, :remote => true %>
And don't forget to update routes.rb too, e.g.:
get "/blablabla", :to => "line_items#create"
After a bit of try and error i found that :
<%= link_to ("<div>Ajouter au panier</div>"+image_tag("some.jpg")).html_safe,
line_items_path(:product_id => #product),
:action => :create,
:remote => true,:method => :post%>
It works perfectly fine!

switch images with onclick

I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id}, :multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete", :id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar, :title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Are you sure the error is coming from the partial? You are using entity_id in the form_tag. Where is it being defined? title isn't used in the partial. It is used inside of legend though. Is that defined?

Resources