【发布时间】:2011-02-06 23:23:48
【问题描述】:
这是独家新闻:我创建了一个测试应用程序,它允许用户创建想法,然后为这些想法添加“气泡”。目前,气泡只是文本。我已经成功地将气泡与想法联系起来。此外,当用户查看一个想法时,它会列出所有附加到该想法的气泡。用户甚至可以删除给定想法的气泡。
我的问题在于编辑气泡。当用户查看一个想法时,他会看到该想法的内容以及该想法的任何气泡。结果,我在想法“显示”视图中设置了所有气泡控件(编辑和删除)。我为想法编辑气泡的代码是<%= link_to 'Edit Bubble', edit_idea_bubble_path %>。我运行rake routes 来找到编辑气泡的正确路径,这就是列出的内容。
这是我的错误:No route matches {:action=>"edit", :controller=>"bubbles"}
在我的气泡控制器中,我有:
def edit
@idea = Idea.find(params[:idea_id])
@bubble = @idea.bubbles.find(params[:id])
end
def update
@idea = Idea.find(params[:idea_id])
@bubble = @idea.bubbles.find(params[:id])
respond_to do |format|
if @bubble.update_attributes(params[:bubble])
format.html { redirect_to(@bubble,
:notice => 'Bubble was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "Edit" }
format.xml { render :xml => @bubble.errors,
:status => :unprocessable_entity }
end
end
end
为了更进一步,我的routes.rb 文件中有以下内容
resources :ideas do
resources :bubbles
end
到目前为止,除了我尝试编辑气泡外,一切似乎都正常工作。
我希望得到一些指导。
谢谢!
这是我的 show.html.erb 创意文件:
<h2>Bubbles</h2>
<% @idea.bubbles.each do |bubble| %>
<p>
<b>Bubble:</b>
<%= bubble.description %>
</p>
<p>
<%= link_to 'Edit Bubble', edit_idea_bubble_path (@idea) %>
</p>
<tb />
<p>
<%= link_to 'Delete Bubble', [bubble.idea, bubble],
:confirm => 'Are you sure you want to delete this bubble?',
:method => :delete %>
</p>
<% end %>
<h2>Add a bubble:</h2>
<%= form_for([@idea, @idea.bubbles.build]) do |f| %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %> </div><% end %>
在 edit_idea_bubble_path (@idea) 之后,这里是 Bubbles 的 edit.html.erb 文件:
<%= render 'form' %>
<%= link_to 'Back to Ideas', ideas_path %>
最后,我的 _form.html.erb Bubbles 文件:我相信这就是问题所在
<% form_for([@idea, @idea.bubbles.build]) do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 forms routes