如何在 Rails 中将参数从视图传递到控制器
How to pass the parameter from view to controller in Rails
我是 Rails 的新手,我正在为 "tasklist"(待办事项列表)开发一个简单的应用程序。
在我的应用程序中,我想根据不同类型的类别(购物、待办事项 - 用户可以创建自己的类别)对任务进行分类。所以我为 User
、Category
和 Task
创建了单独的模型,每个 Task
都与一个 category
相关联。
在我看来(users/show.html.erb
-n side this I render view for category
and task
),我在左侧列出了所有类别,在左侧列出了所有未完成的任务右边。我想将类别设为 LINKS,因此当用户 select 一个类别时,只有链接到该类别类型的任务才会显示在右侧。
我了解正常 link_to 进入新页面时的工作方式。我也了解按钮在 bootstrap 中的工作原理。但是我无法确定如何将类别 selection 传递到控制器中,因此我只能提取类别与用户 selected 匹配的任务。
感谢您的帮助。
您可以像这样在 link_to
中传递 category
:
link_to "Task for test_category", task_path(category: "test_category")
然后,您可以在控制器中使用 params[:category]
抓取类别并根据需要使用它。
也许你应该创建一个新路由而不是传递参数。
resources(:categories) do
member do
get(:linked_tasks)
end
end
在控制器类别中
def linked_tasks
category = Category.find(params[:id])
tasks = category.tasks.where(is_linked: true)
end
希望对您有所帮助。
您需要将 category
参数传递给某种评估函数,过滤适当的类别。这可以通过 Javascript
或 Rails
:
来完成
Rails
<% Category.all.each do |category| %>
<%= link_to category.name, users_path(user, category: category.id) %>
<% end %>
这会将 category
参数传递给您的 users#show
操作:
def show
@user = User.find params[:id]
@categories = @user.categories
@categories = @categories.where(category: params[:category]) if params[:category]
end
Javascript
如果您想进行探索,您会希望使用 JS 仅在此视图上保留您的功能:
#app/assets/javascripts/application.js
$(document).on("click", "a.category", function(e){
var id = $(this).data("category_id");
$(".todos a").not("[data-category_id=\"" + id + \""]").hide();
});
#app/views/users/show.html.erb
<% Category.all.each do |category| %>
<%= link_to category.name, users_path(user, category: category.id), class: "category", data: { category_id: category.id } %>
<% end %>
<div class="todos">
<% @user.todos.each do |todo| %>
<%= link_to todo.title, todo, data: { category_id: todo.category_id } %>
<% end %>
</div>
这将允许您单击 "category" 并更改 "todos" 列表以适应。
我是 Rails 的新手,我正在为 "tasklist"(待办事项列表)开发一个简单的应用程序。
在我的应用程序中,我想根据不同类型的类别(购物、待办事项 - 用户可以创建自己的类别)对任务进行分类。所以我为 User
、Category
和 Task
创建了单独的模型,每个 Task
都与一个 category
相关联。
在我看来(users/show.html.erb
-n side this I render view for category
and task
),我在左侧列出了所有类别,在左侧列出了所有未完成的任务右边。我想将类别设为 LINKS,因此当用户 select 一个类别时,只有链接到该类别类型的任务才会显示在右侧。
我了解正常 link_to 进入新页面时的工作方式。我也了解按钮在 bootstrap 中的工作原理。但是我无法确定如何将类别 selection 传递到控制器中,因此我只能提取类别与用户 selected 匹配的任务。
感谢您的帮助。
您可以像这样在 link_to
中传递 category
:
link_to "Task for test_category", task_path(category: "test_category")
然后,您可以在控制器中使用 params[:category]
抓取类别并根据需要使用它。
也许你应该创建一个新路由而不是传递参数。
resources(:categories) do
member do
get(:linked_tasks)
end
end
在控制器类别中
def linked_tasks
category = Category.find(params[:id])
tasks = category.tasks.where(is_linked: true)
end
希望对您有所帮助。
您需要将 category
参数传递给某种评估函数,过滤适当的类别。这可以通过 Javascript
或 Rails
:
Rails
<% Category.all.each do |category| %>
<%= link_to category.name, users_path(user, category: category.id) %>
<% end %>
这会将 category
参数传递给您的 users#show
操作:
def show
@user = User.find params[:id]
@categories = @user.categories
@categories = @categories.where(category: params[:category]) if params[:category]
end
Javascript
如果您想进行探索,您会希望使用 JS 仅在此视图上保留您的功能:
#app/assets/javascripts/application.js
$(document).on("click", "a.category", function(e){
var id = $(this).data("category_id");
$(".todos a").not("[data-category_id=\"" + id + \""]").hide();
});
#app/views/users/show.html.erb
<% Category.all.each do |category| %>
<%= link_to category.name, users_path(user, category: category.id), class: "category", data: { category_id: category.id } %>
<% end %>
<div class="todos">
<% @user.todos.each do |todo| %>
<%= link_to todo.title, todo, data: { category_id: todo.category_id } %>
<% end %>
</div>
这将允许您单击 "category" 并更改 "todos" 列表以适应。