link_to :action =>“创建"将索引而不是“创建" [英] link_to :action => 'create' going to index rather than 'create'
问题描述
我正在构建一个相当简单的食谱应用程序来学习 RoR,我试图允许用户通过单击链接而不是通过表单来保存食谱,因此我通过一个连接 user_recipe 控制器的创建"功能链接到.
不幸的是,由于某种原因,link_to 调用的是索引函数而不是创建函数.
我已经把link_to写成
<前><%= "保存这个食谱", :action => 'create', :recipe_id => @recipe %>此链接位于 user_recipes/index.html.erb 上,正在调用同一控制器的创建"函数.如果我包含 :controller 似乎没有什么区别.
控制器看起来像这样
<前>定义索引@recipe = params[:recipe_id]@user_recipes = UserRecipes.all # 更改以查找当数据库中有多个用户时response_to do |格式|format.html #index.html.erbformat.xml { 渲染:xml => @recipes }结尾结尾定义创建@user_recipe = UserRecipe.new@user_recipe.recipe_id = params[:recipe_id]@user_recipe.user_id = current_userresponse_to do |格式|如果@menu_recipe.saveformat.html { redirect_to(r, :notice => '菜单创建成功.') }format.xml { render :xml => @menu, :status => :created, :location => @menu }别的format.html { 渲染:动作 =>新"}format.xml { 渲染 :xml => @menu.errors, :status => :unprocessable_entity }结尾结尾在标准 REST 方案中,索引操作和创建操作都具有相同的 url (/recipes
),唯一的不同在于index 使用 GET 访问,create 使用 POST 访问.所以 link_to :action =>:create
将简单地生成一个指向 /recipes
的链接,这将导致浏览器在点击时对 /recipes
执行 GET 请求,从而调用索引操作.
要调用创建操作,请使用 link_to {:action =>;:create}, :method =>:post
,明确告诉 link_to
您想要发布请求,或者使用带有提交按钮而不是链接的表单.
I am building a fairly simple recipe app to learn RoR, and I am attempting to allow a user to save a recipe by clicking a link rather than through a form, so I am connecting the user_recipe controllers 'create' function through a link_to.
Unfortunately, for some reason the link_to is calling the index function rather than the create.
I've written the link_to as
<%= "save this recipe", :action => 'create', :recipe_id => @recipe %>
this link is on the user_recipes/index.html.erb and is calling the 'create' function of the same controller. It doesn't seem to make a difference if I include the :controller or not.
The controllers look like this
def index @recipe = params[:recipe_id] @user_recipes = UserRecipes.all # change to find when more than one user in db respond_to do |format| format.html #index.html.erb format.xml { render :xml => @recipes } end end def create @user_recipe = UserRecipe.new @user_recipe.recipe_id = params[:recipe_id] @user_recipe.user_id = current_user respond_to do |format| if @menu_recipe.save format.html { redirect_to(r, :notice => 'Menu was successfully created.') } format.xml { render :xml => @menu, :status => :created, :location => @menu } else format.html { render :action => "new" } format.xml { render :xml => @menu.errors, :status => :unprocessable_entity } end end
In the standard REST scheme the index action and the create action both have the same url (/recipes
) and only differ in that index is accessed using GET and create is accessed using POST. So link_to :action => :create
will simply generate a link to /recipes
which will cause the browser to perform a GET request for /recipes
when clicked and thus invoke the index action.
To invoke the create action use link_to {:action => :create}, :method => :post
, telling link_to
explicitly that you want a post request, or use a form with a submit button rather than a link.
这篇关于link_to :action =>“创建"将索引而不是“创建"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!