将参数传递给rjs文件

时间:2012-07-05 13:24:42

标签: ruby-on-rails

如果有人能帮助我解决以下问题,我将不胜感激。

我有以下表格:

new_book表格

<% remote_form_for @book, :url => { :controller => "books", :action => "create_sub_book"} do |f| %>
    <%= f.text_field :book_name %>
    <%= f.text_field :book_publication %>

    <%= params[:id] %> <!-- I need to pass this value to create_sub_book action, which will then pass it to create_sub_book.js.rjs -->

    <p><%= f.submit 'Create'%></p>

<% end %>

* 控制器操作*

def create_sub_book

@book = book.new(params[:book])

respond_to do |format|
  if @book.save
    format.js
  else
    flash[:notice] = "Book failed to save."
  end
end
end

** create_sub_book.js.rjs **

`page.alert(params[:id]) ## On this page I need to access the params[:id] on the 'new_book' form. This line does not work`.

关于如何将'new_book'表单上的params [:id]传递给'create_sub_book.js.rjs'的任何建议?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以使用hidden_field标记。

<%= f.hidden_field :id, params[:id] %>

这将插入你的params散列,然后你就可以传递它。