简体   繁体   中英

How can I get value from partial?

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(params[: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] .

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