Ruby-on-rails link_to :action => 'create' 去索引而不是 'create'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3738573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:13:33  来源:igfitidea点击:

link_to :action => 'create' going to index rather than 'create'

ruby-on-railslink-to

提问by pedalpete

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.

我正在构建一个相当简单的配方应用程序来学习 RoR,并且我试图允许用户通过单击链接而不是通过表单来保存配方,因此我通过 link_to 连接 user_recipe 控制器“创建”功能。

Unfortunately, for some reason the link_to is calling the index function rather than the create.

不幸的是,由于某种原因,link_to 调用的是索引函数而不是创建函数。

I've written the link_to as

我已经将 link_to 写为

<%= "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.

此链接位于 user_recipes/index.html.erb 上,正在调用同一控制器的“创建”功能。如果我包含 :controller 似乎没有什么区别。

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

回答by sepp2k

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 => :createwill simply generate a link to /recipeswhich will cause the browser to perform a GET request for /recipeswhen clicked and thus invoke the index action.

在标准的 REST 方案中,索引操作和创建操作都具有相同的 url ( /recipes),唯一的不同在于索引是使用 GET 访问的,而创建是使用 POST 访问的。Solink_to :action => :create将简单地生成一个链接,/recipes该链接将导致浏览器/recipes在单击时执行 GET 请求,从而调用索引操作。

To invoke the create action use link_to {:action => :create}, :method => :post, telling link_toexplicitly that you want a post request, or use a form with a submit button rather than a link.

要调用 create 操作,请使用link_to {:action => :create}, :method => :postlink_to明确告知您想要发布请求,或使用带有提交按钮而不是链接的表单。

回答by Yusuf Saber

Assuming you have default resources set up in your routes file, i.e. something like this

假设您在路由文件中设置了默认资源,即像这样

resources :recipes

The following will generate a link that will create a recipe; i.e. will be routed to the create action.

以下将生成将创建配方的链接;即将被路由到创建操作。

<%= link_to "Create Recipe", recipes_path, :method => :post %>

For this to work, JS needs to be enabled in your browser.

为此,需要在浏览器中启用 JS。

The following will generate a link that will show all recipes; i.e. will be routed to the index action.

以下将生成一个显示所有食谱的链接;即将被路由到索引操作。

<%= link_to "All Recipes", recipes_path %>

This assumes the default which is a Get HTTP request.

这假定默认值为 Get HTTP 请求。