具有belongs_to和has_many关系的脚手架:“编辑”的link_to路径和“摧毁”不工作

发布于 2024-10-31 09:55:42 字数 2473 浏览 0 评论 0原文

我的项目允许家庭页面有多个故事。这些模型包括关系 - “家庭有很多故事”和“故事属于家庭”。在routes.rb文件中,我有以下内容:

resources :families do
    resources :stories   
  end

为故事控制器生成以下路由:

    family_stories GET    /families/:family_id/stories(.:format)          {:action=>"index", :controller=>"stories"}
                   POST   /families/:family_id/stories(.:format)          {:action=>"create", :controller=>"stories"}
  new_family_story GET    /families/:family_id/stories/new(.:format)      {:action=>"new", :controller=>"stories"}
 edit_family_story GET    /families/:family_id/stories/:id/edit(.:format) {:action=>"edit", :controller=>"stories"}
      family_story GET    /families/:family_id/stories/:id(.:format)      {:action=>"show", :controller=>"stories"}
                   PUT    /families/:family_id/stories/:id(.:format)      {:action=>"update", :controller=>"stories"}
                   DELETE /families/:family_id/stories/:id(.:format)      {:action=>"destroy", :controller=>"stories"}

以下是相关的控制器方法:

  def edit
    @story = @family.stories.find(params[:id])
  end

  def destroy
    @story = @family.stories.find(params[:id])
    @story.destroy
    redirect_to family_stories_url, :notice => "Successfully destroyed story."
  end

对于index.html.erb,nifty脚手架生成的代码不考虑关系,并且“显示”、“编辑”和“销毁”的链接不起作用。经过一番研究,我修改了这些链接的代码如下:

  <% for story in @stories %>
    <tr>
      <td><%= story.title %></td>
      <td><%= story.body %></td>
      <td><%= link_to "Show", [@family, story] %></td>
      <td><%= link_to "Edit", edit_family_story_path([@family, story]) %></td>
      <td><%= link_to "Destroy", [@family,story], :confirm => 'Are you sure?', :method => :delete %> </td>
    </tr>
  <% end %>

当用“[@family,story]”替换原始“story”变量时,“Show”链接工作正常。 “销毁”和“编辑”链接不适用于类似的替换。

“销毁”链接不会出现错误,但其作用与“显示”链接类似 - 记录不会被删除,而是显示(:confirm 对话框从不显示。)“编辑”链接会生成以下错误:

"No route matches {:action=>"edit", :controller=>"stories", :family_id=>\#[Story id: 1, title: "story01 for family01", body: "body01 for story01 for family01", created_at: "2011-04-09 22:55:14", updated_at: "2011-04-09 22:55:14", family_id: 1]}"

[@ family,story] ​​构造对于“Show”链接效果很好。为什么它不适用于“编辑”和“销毁”链接?我该如何修改它们才能正常工作?

My project allows a family page to have multiple stories. The models include the relationships - "Family has_many stories" and "Story belongs to family". In the routes.rb file I have the following:

resources :families do
    resources :stories   
  end

Resulting in the following routes for the stories controller:

    family_stories GET    /families/:family_id/stories(.:format)          {:action=>"index", :controller=>"stories"}
                   POST   /families/:family_id/stories(.:format)          {:action=>"create", :controller=>"stories"}
  new_family_story GET    /families/:family_id/stories/new(.:format)      {:action=>"new", :controller=>"stories"}
 edit_family_story GET    /families/:family_id/stories/:id/edit(.:format) {:action=>"edit", :controller=>"stories"}
      family_story GET    /families/:family_id/stories/:id(.:format)      {:action=>"show", :controller=>"stories"}
                   PUT    /families/:family_id/stories/:id(.:format)      {:action=>"update", :controller=>"stories"}
                   DELETE /families/:family_id/stories/:id(.:format)      {:action=>"destroy", :controller=>"stories"}

Here are the relevant controller methods:

  def edit
    @story = @family.stories.find(params[:id])
  end

  def destroy
    @story = @family.stories.find(params[:id])
    @story.destroy
    redirect_to family_stories_url, :notice => "Successfully destroyed story."
  end

For index.html.erb the code generated by nifty scaffolding does not take into account the relationships, and the links for the "Show", "Edit", and "Destroy" do not work. After some research I modified the code for these links as follows:

  <% for story in @stories %>
    <tr>
      <td><%= story.title %></td>
      <td><%= story.body %></td>
      <td><%= link_to "Show", [@family, story] %></td>
      <td><%= link_to "Edit", edit_family_story_path([@family, story]) %></td>
      <td><%= link_to "Destroy", [@family,story], :confirm => 'Are you sure?', :method => :delete %> </td>
    </tr>
  <% end %>

The "Show" link works fine when replacing the original "story" variable with "[@family, story]". The "Destroy" and "Edit" links do not work with similar substitutions.

The "Destroy" link develops no errors, but acts just like the "Show" link - the record is not deleted and is instead displayed (the :confirm dialog never shows.) The "Edit" link generates the following error:

"No route matches {:action=>"edit", :controller=>"stories", :family_id=>\#[Story id: 1, title: "story01 for family01", body: "body01 for story01 for family01", created_at: "2011-04-09 22:55:14", updated_at: "2011-04-09 22:55:14", family_id: 1]}"

The [@family,story] construct works well for the "Show" link. Why doesn't it work for the "Edit" and "Destroy" links? How do I modify them to work correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无声无音无过去 2024-11-07 09:55:42

这是由 javascript 配置冲突引起的。

我使用的 Rails 应用程序生成器将 jquery.js 文件放在“public/javascript”文件夹中,但在一个文件夹中调用 jquery(我必须将其上移一个级别。)2) config/application.rb 文件有两个JavaScript 配置行。我删除了一个并确保第二个指向上面提到的 jquery.js 文件。有了这些改变,一切都奏效了

This was caused by a conflicting javascript configuration.

The rails application builder that I used put my jquery.js file in the "public/javascript" folder, but within a folder call jquery (I had to move it up a level.) 2) the config/application.rb file had two javascript configuration lines. I deleted one and made sure the second one pointed to the jquery.js file mentioned above. With those to changes all worked

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