Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 单击时将“link_”更新为“helper”;轨道4_Ruby On Rails 4_Link To - Fatal编程技术网

Ruby on rails 4 单击时将“link_”更新为“helper”;轨道4

Ruby on rails 4 单击时将“link_”更新为“helper”;轨道4,ruby-on-rails-4,link-to,Ruby On Rails 4,Link To,在view.html.erb中,我想使用如下内容: <tbody> <% @books.each do |book| %> <tr> <td><%= book.title %></td> <td><%= book.author %></td> <td><%= link_to 'Show', book,

在view.html.erb中,我想使用如下内容:

  <tbody>
    <% @books.each do |book| %>
      <tr>
        <td><%= book.title %></td>
        <td><%= book.author %></td>
        <td><%= link_to 'Show', book, class: 'btn btn-mini btn-primary' %></td>
        <td><%= link_to 'Edit', edit_book_path(book), class: 'btn btn-default' %></td>
        <td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-mini btn-danger' %></td>
        <td><%= link_to 'Reserve it', books_path(book, :situation_id => 2), class: "btn btn-mini btn-success" %></td>
      </tr>
    <% end %>
  </tbody>

2) ,类别:“btn小型btn成功”%>
当我点击“预订”按钮时,我希望本书的“情境id”字段等于2


我该怎么做?

如果一本书有一个情境关联(并且您的关联设置正确),那么您可以将其作为参数传递给book.situation\u id),…%>

但是,您不需要将situation_id param传递给它,因为您可以使用将要使用的book实例变量获取它。换句话说,你能做到

def show
  @book = Book.find(params[:id]).include(:situation)
end

您的书也将加载这种情况,并将在您的书展视图中提供。

不是Rails解决方案,但我希望这将帮助您朝着正确的方向思考

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#link").click(function(){
$("a").attr("href", function(i, href) {
  return href + 'books/?situation_id=2';
});
    });
});
</script>
</head>
<body>
<a href="" id="link" >hello</a>
</body>
</html>

$(文档).ready(函数(){
$(“#链接”)。单击(函数(){
$(“a”).attr(“href”,函数(i,href){
return href+'books/?situation\u id=2';
});
});
});

不确定这是如何回答问题的。听起来OP想在单击按钮后将书本的情境id设置为2。是的,我想在单击按钮时将书本的情境id更改为2(之前设置为1)。情境id已与书本正确关联。我没有成功地做到这一点。谢谢你的回答我不太确定,但请再点击一次;也许对你有帮助。就像您可以尝试使用javascript推送情境_id=2的值一样。你已经试过这样做了吗?谢谢你的回答。我没有按你说的做。你是怎么做到的?很抱歉,我是rails的初学者,也是英语的初学者(对不起)。既然你是新来的,我想让你读一读:。试着用简短但信息丰富的文字写下你们问题的标题。