Friday 15 April 2011

ruby - How to connect two exisitng Objects in Rails via onclick? -


I have user model and car model i
cars for every car / index.html.erb Show the add button so that the user can add any car And in the user / show.html.erb I want to list all the cars they said.
The problem is that it connects not only the clickable car to other cars in cars / index.html .erb


user.erb and car.erb

  class users & lt; & Lt; ActiveRecord :: Base is_may: car and class car & lt; & Lt; ActiveRecord :: Base is_to: User End  


car / index.html.erb

  and <% @ car. Car | & Gt%; # ... & lt; Td> & Lt;% = link_to 'Take Car', '#',: OnClick = & gt; Get_car (car.id)%> & Lt; / Td> # ... & lt;% end% & gt;  



cars_helper.rb

  def get_car (id) current_user.cars & lt; & Lt; Car.find_by (id: id) and  


user / show.html.erb

  & lt;% if @username.cars Any? & Gt%; & Lt;% @ user.cars.each do | C | & Gt%; & Lt;% = c.name% & gt; & Lt;% end% & gt;  

Something like this:

car / index.html. ARB

  & lt;% @ car.each do | Car | & Gt%; # ... and lt;% = link_to 'Take Car', get_car_path (: id => car.id)%> # ... & lt;% end% & gt;  

cars_controllers.rb

  def get_car @user = current_user @ user.cars = car.find (paramas [: id]) response_to do | Format | If the @ user.save format.html {redirect_to cars_url} format.json {head: no_content} end and end  

routes.rb

  match '/ Cars / get_car '= & gt; 'Car # get_car' ,: as = & gt; : Get_car  

No comments:

Post a Comment