link_to : 操作 => “创建”去索引而不是“创建”

发布于 2024-09-19 18:09:31 字数 1187 浏览 9 评论 0原文

我正在构建一个相当简单的食谱应用程序来学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-09-26 18:09:32

在标准 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. 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.

三五鸿雁 2024-09-26 18:09:32

假设您在路由文件中设置了默认资源,即类似以下内容的内容,

resources :recipes

以下将生成一个链接,该链接将创建一个配方; ie 将被路由到创建操作。

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

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

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

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文