I use a partial as part of my form to create a new task. The partial is a dynamic list (AJAX) of categories so user can select an appropriate category.
I need to have access to categories/category_list/@current_category variable from tasks_controller to assign selected category to a task.
new.html.erb
<%= form_for @task do |form| %>
Category:
<%= render :partial => 'categories/category_list', :object => Category.root_list %>
...
<% end %>
tasks_controller.rb
def create
@task = Task.new(para开发者_开发技巧ms[:task])
@task.category_id = ????
@task.save
...
end
Thank you for your help!
If I understand you correctly, you want to access the value of HTML field and this field is generated dynamically, but submitted statically. That does not differ from having it done without AJAX.
Given the name of the field you can access it with params[:name_of_field]
. You can also manually specify the name of the field to be, in your case, task[category_id]
, in which case you can omit the line with ??? from your controller, as it will be covered by params[:task]
.
精彩评论