开发者

No route matches when trying to edit

开发者 https://www.devze.com 2023-02-09 12:00 出处:网络
Here\'s the scoop: I\'ve created a test app that allows users to create ideas and then add \"bubbles\" to these ideas. Currently, a bubble is just text. I\'ve successfully linked bubbles to ideas. Fur

Here's the scoop: I've created a test app that allows users to create ideas and then add "bubbles" to these ideas. Currently, a bubble is just text. I've successfully linked bubbles to ideas. Furthermore, when a user goes to view an idea it lists all of the bubbles attached to that idea. The user can even delete the bubble for a given idea.

My problem lies in editing bubbles. When a user views an idea, he sees the idea's content as well as any bubbles for that idea. As a result, I've set all my bubble controls (editing and deleting) inside the ideas "show" view. My code for editing a bubble for an idea is <%= link_to 'Edit Bubble', edit_idea_bubble_path %>. I ran rake routes to find the correct path for editing bubbles and that is what was listed.

Here's my error: No route matches {:action=>"edit", :controller=>"bubbles"}

In my bubbles controller I have:

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

To go a step further, I have the following in my routes.rb file

resources :ideas do
  resources :bubbles
end

So far everything seems to function except when I try to edit a bubble.

I'd love some guidance.

Thanks!

Here's my show.html.erb file for Ideas:

<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 /开发者_如何学Python>  
<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 %>

Following edit_idea_bubble_path (@idea), here is the edit.html.erb file for Bubbles:

<%= render 'form' %>
<%= link_to 'Back to Ideas', ideas_path %>

And finally, my _form.html.erb file for Bubbles: this is where the problem lies, I believe

<% 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 %>


First build the route in your show.html.erb file

<%= link_to 'Edit Bubble', edit_idea_bubble_path(@idea, bubble) %>

Then, your controller should have 2 objects for both @idea and @bubble

when action is new

@idea = Idea.find_by_id(:params[:idea_id])
@bubble = @idea.bubbles.build

when action is edit

@idea = Idea.find_by_id(:params[:idea_id])
@bubble = @idea.bubbles.find_by_id(:params[:bubble_id])

In your _form.html.erb

<% form_for([@idea, @bubble]) do |f| %>


You have to provide the idea and the bubble objects:

<%= link_to 'Edit Bubble', edit_idea_bubble_path(@idea,@bubble) %>

Update:

Edit the _form.html.erb file

<% form_for([@idea, @bubble]) do |f| %> 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号