以下是独家新闻:我创建了一个测试应用程序,允许用户创建想法,然后为这些想法添加“气泡”。目前,泡沫只是文字。我已经成功地将泡沫与想法联系起来。此外,当用户去查看想法时,它会列出与该想法相关的所有气泡。用户甚至可以删除给定想法的气泡。
我的问题在于编辑气泡。当用户观看一个想法时,他会看到该想法的内容以及该想法的任何气泡。因此,我在想法“show”视图中设置了所有的气泡控件(编辑和删除)。我为一个想法编辑气泡的代码是<%= 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 %>
答案 0 :(得分:1)
首先在show.html.erb文件中构建路径
<%= link_to 'Edit Bubble', edit_idea_bubble_path(@idea, bubble) %>
然后,你的控制器应该有2个对象@idea和@bubble
当行动是新的
时@idea = Idea.find_by_id(:params[:idea_id])
@bubble = @idea.bubbles.build
动作编辑时
@idea = Idea.find_by_id(:params[:idea_id])
@bubble = @idea.bubbles.find_by_id(:params[:bubble_id])
在_form.html.erb
中<% form_for([@idea, @bubble]) do |f| %>
答案 1 :(得分:0)
您必须提供构思和气泡对象:
<%= link_to 'Edit Bubble', edit_idea_bubble_path(@idea,@bubble) %>
<强>更新强>
编辑_form.html.erb文件
<% form_for([@idea, @bubble]) do |f| %>