link_to : 操作 => “创建”去索引而不是“创建”
我正在构建一个相当简单的食谱应用程序来学习 RoR,并且我试图允许用户通过单击链接而不是通过表单来保存食谱,因此我通过 link_to 连接 user_recipe 控制器“创建”功能。
不幸的是,由于某种原因,link_to 正在调用索引函数而不是创建函数。
我已经编写了 link_to,因为
<%= "save this recipe", :action => 'create', :recipe_id => @recipe %>
此链接位于 user_recipes/index.html.erb 上,并且正在调用同一控制器的“create”函数。如果我包含 :controller 似乎没有什么区别。
控制器看起来像这样
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在标准 REST 方案中,索引操作和创建操作都具有相同的 url (
/recipes
),唯一的区别在于索引是使用 GET 访问的,而创建是使用 POST 访问的。所以link_to :action => :create
将简单地生成一个指向/recipes
的链接,这将导致浏览器在单击时对/recipes
执行 GET 请求,从而调用索引操作。要调用创建操作,请使用 link_to {:action => :创建},:方法=> :post,明确告诉
link_to
您想要一个发布请求,或者使用带有提交按钮而不是链接的表单。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. Solink_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
, tellinglink_to
explicitly that you want a post request, or use a form with a submit button rather than a link.假设您在路由文件中设置了默认资源,即类似以下内容的内容,
以下将生成一个链接,该链接将创建一个配方; ie 将被路由到创建操作。
为此,需要在浏览器中启用 JS。
以下将生成一个显示所有食谱的链接; ie 将被路由到索引操作。
这假定默认值是 Get HTTP 请求。
Assuming you have default resources set up in your routes file, i.e. something like this
The following will generate a link that will create a recipe; i.e. will be routed to the create action.
For this to work, JS needs to be enabled in your browser.
The following will generate a link that will show all recipes; i.e. will be routed to the index action.
This assumes the default which is a Get HTTP request.