0

I'm a new rails developer who's creating an application that creates the following issue:

Routing Error

No route matches {:action=>"show", :controller=>"recipes", :username=>#<Recipe id: 42, name: "Curried Chicken Sandwich", yield: "4 Servings", description: "Burgers aren't the only grilled things we want to e...", created_at: "2013-06-27 18:49:42", updated_at: "2013-06-27 18:49:42", duration: "", author: nil, url: nil, user: 3, image: "curried-chicken-sandwich-646.jpg", user_recipe_id: nil>}

My routes.rb looks like this:

match "/:username" => "recipes#index"
scope ':username' do
  resources :recipes
end

My controller index looks like:

def index
 if params[:tag]
  @recipes = Recipe.tagged_with(params[:tag]).where(:user => current_user.id).paginate(:page => params[:page], :per_page => 45)
 else
  @user = User.find_by_username params[:username]
  @search = Recipe.search do |query|
    query.fulltext params[:search]
    query.with(:user, @user.id)
    query.paginate(:page => params[:page] || 1, :per_page => 20)
 end
  @recipes = @search.results
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @recipes }
  end
 end
end

and my controller show looks like this:

def show
   @user = User.find_by_username params[:username]
   @recipe = Recipe.where(:user_recipe_id => params[:id])

   respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @recipe }
   end
end

My index view:

<% if not @recipes.any? %>
No Recipes
<% else%>

<% @recipes.each do |recipe| %>

    <h3><%= link_to recipe.name, recipe %></h3>
<% end %>

Why does this happen? The show action is there, but in the error it says it doesn't.

thanks for all help

8
  • 1
    Error is probably thrown by view code. Could you show it? Commented Jul 1, 2013 at 17:39
  • I'm not sure you can use routing scopes that way. Commented Jul 1, 2013 at 17:48
  • where is this error triggered? Commented Jul 1, 2013 at 17:49
  • @MarekLipka, Ok I just updated my question Commented Jul 1, 2013 at 17:49
  • @Lichtamberg, The error shows up on my index page Commented Jul 1, 2013 at 17:49

1 Answer 1

3
<%= link_to recipe.name, recipe_path(username: @user.username, id: recipe.id) %>
Sign up to request clarification or add additional context in comments.

7 Comments

Fix your spelling, and you may also need to pass the actual username and not the user object. But that's basically what you want.
@Lichtamberg, Thanks for the response, but the error still shows up?
@the_ did you try what @GoGoCarl mentioned? Replace @user with @user.username and make sure to spell recipe properly. Does it still not work?
@theIv I just tried GoGoCarl's suggestion, but it still comes up with the same error. If the user has no recipes associated with him/her though, then no error comes up
I went ahead and proposed an edit to the answer just to clarify the comments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.