简体   繁体   中英

Call a controller method, depending on the model instance. Rails

In my program I use gem 'jquery-rails' and I have two controllers:

1. CarsController.

2. CarRoadsController.

For creating cars I have method create in CarsController and create.js.erb file with follow code:

$("#todo-list").append("<%= j(render(@car)) %>");

Also I have index.html.erb file where I have a condition:

<% if @car %>
  <%= link_to(image_tag(road.image.url(:thumb)), car_roads_create_path, method: :post, remote: true) %>
<% else %>
  <%= link_to(image_tag(road.image.url(:thumb)), cars_create_path, method: :post, remote: true) %>
<% end %>

But when I click on image it always call Cars#create even after creating @car.

What should I do to call two different methods, depending on condition?

If you look at your code, in index.html.erb you have:

<% if @car %>
  // this statement will gets executed only when you have @car set in index action of cars controller
  <%= link_to(image_tag(road.image.url(:thumb)), car_roads_create_path, method: :post, remote: true) %>
<% else %>
  // this statement will gets executed when you don't have @car index action of cars controller
  <%= link_to(image_tag(road.image.url(:thumb)), cars_create_path, method: :post, remote: true) %>
<% end %>

And in your index action you don't have @car set so it always takes you to cars create action.

Now lets look at your code in create.js.erb, you have:

$("#todo-list").append("<%= j(render(@car)) %>");

This code doesn't set @car in your index action, it's only a shorthand to render a partial with instance variable . It changes the content inside element with id="todo-list" by appending code inside partial _car.html.erb and passing @car variable in that partial

FIX:

To fix your issue you need to change your link in create.js.erb, like this:

#index.html.erb
<div id="create-link">
  <%= link_to(image_tag(road.image.url(:thumb)), cars_create_path, method: :post, remote: true) %>
</div>

#_create_road_car.html.erb
<%= link_to(image_tag(road.image.url(:thumb)), car_roads_create_path, id: "roads_create") %>

#create.js.erb
$("#todo-list").append("<%= j(render(@car)) %>");
$("#create-link").html("<%=j render partial: "create_road_car", locals: {road: @your_road_variable}  %>");

Update:

Based on our chat you can do it like this:

#index.html.erb
<div id="create-link">
  <%= link_to(image_tag(road.image.url(:thumb)), cars_create_path(road: road), method: :post, remote: true) %>
</div>

#car_roads_controller.rb
def create
  #other code
  @road = params[:road]
end

#create.js.erb
$("#todo-list").append("<%= j(render(@car)) %>");
$("#create-link").html("<%=j render partial: "create_road_car", locals: {road: @road}  %>");

You are POSTing via a link_to... depending on how your routes are set up, you probably aren't able to access the route you are trying to get to.

can you show us the routes you setup for these links in your config/routes.rb?

Are you using the link_to to submit a form? if so, you should use jquery to do so by assigning ids to your forms and the links, like so:

<% if @car %>
  <%= link_to(image_tag(road.image.url(:thumb)), car_roads_create_path, id: "roads_create") %>
<% else %>
  <%= link_to(image_tag(road.image.url(:thumb)), cars_create_path, id: "car_create") %>
<% end %>

<script type='text/javascript'>
  $("#roads_create").click(function(){
    $("#road_create_form").submit();
  });
  $("#car_create").click(function(){
    $("#car_create_form").submit();
  });
</script>

If on the other hand, you don't have any forms to submit, you should use a GET request with a parameter in the link_to to pass in the data.

If you provide more information, we can provide more detailed assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM