0

Hi guys in my index view page, i'm having this error in this line :

<td id="change"><%= link_to 'Analyze',user_generator_path(current_user.generators)%></td>

The code for that line is :

        <% if generator.result.present?%>
            <td>               <%= generator.result.ncbi_ref_seq %></td>
            <td>               <%= generator.result.genome_sample %></td>
            <td align="center"><%= generator.result.binding_times %></td>
            <td id="change"></td>
            <td id="change"><%= link_to 'Delete', generator, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% else %>
            <td></td>
            <td></td>
            <td></td>
            <td id="change"><%= link_to 'Analyze',user_generator_path(current_user.generators)%></td>
            <td id="change"><%= link_to 'Delete', generator, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% end %>

I'm getting

No route matches {:action=>"show", :controller=>"generators", :user_id=>#<ActiveRecord::Associations::CollectionProxy [#<Generator id: 1, primer_length: 20, no_A: nil, no_T: nil, no_G: nil, no_C: nil, melting_temp: nil, choice: nil, random_primer_generated: nil, user_seq: nil, f_primer: nil, r_primer: nil, result_choice: nil, user_id: 1, created_at: "2013-12-09 09:27:39", updated_at: "2013-12-09 09:27:39">]>, :id=>nil, :format=>nil} missing required keys: [:id]

Generator.rb

def new
    @generator = current_user.generators.build(params[:generator])
  end

def create    
    @generator = current_user.generators.build(params[:generator])
    @generator.user_id = current_user.id if current_user
    @generator.choice = params[:choice]
      if params[:choice] == 'Randomly'
          @generator.random_generate(generator_params)
      elsif params[:choice] == 'Specified ATGC'
          @generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
      elsif params[:choice] == 'Seating'
          @generator.seating(params[:user_seq])
      end

    @generator.result_choice=params[:result_choice]
    @generator.save
    respond_to do |format|
       if @generator.result_choice == 'Yes'
              format.html { redirect_to(user_generator_path(@generator)) }
       else
              format.html { redirect_to(user_generators_path(@generator) ) }
       end
     end 

  end

How do i fix it ? i just want to display user's generators. if there's result, then user.generators.result

Route.rb

root :to => 'welcome#index' 
  get '/auth/:provider/callback' => 'sessions#create' 
  post '/auth/identity/callback' => 'sessions#create'

  resources :users do
    resources :generators 
    resources :results
  end  
  resources :identities
BC2
  • 1,129
  • 1
  • 11
  • 22

1 Answers1

1

To me, it looks like this is the culprit:

:id=>nil

We've had this problem with nested routes before (where you have /users/15/articles/215/edit)


This is an immediate fix:

user_generator_path(:user_id => current_user.generators, :id => variable_here)

However, it seems there is a deeper issue with your system, namely that you've created what seems to be a member route, which needs an id parameter to get it working correctly


Systemic Fix

You mention you "just want to display user's generators", which would make an index collection route for the user's generators?

The way to display this is to firstly change the link to open the index action of the generators controller. Currently, your controller shows you're either loading the new or create actions, and your error pertains to opening the show action. The show action in particular requires the id of the item, which is where this error is coming form

From the sounds of it, I'd just route to the index path for users_generators_path. There you can display the generators for the user without a requirement for the specific id

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • yes user can generate genrators . But the error is returning me null for the generated things. 1 user has many generators. Generator has_one result – BC2 Dec 09 '13 at 09:57
  • well actually the reason for putting show is so that user can link the result to generator. Let's say user does only generator and later on they wanna find out the result, so they just click analyze and it will direct to show and inside show, they can create result and link with the generator. so in the end , i will get user.generator.result – BC2 Dec 09 '13 at 10:07
  • Okay, so what you really want to show is the generator's result? If that's the case, you'll need to amend your routes to read like this: `resources :users do resources :generators do resources :results end end` – Richard Peck Dec 09 '13 at 10:10
  • but user can first generate a generator first. and using the generator as a referene, result can then use generator and get my final output. – BC2 Dec 09 '13 at 10:12
  • but from my error, the generator is not even generating anything. it returns nil. But user_id and generator_id is there – BC2 Dec 09 '13 at 10:13
  • It seems the error is for the route? The route is not recognizing the `id` variable for it? – Richard Peck Dec 09 '13 at 10:15
  • i've tried putting <%= link_to 'Analyze',user_generator_path(generator.id)%> this returns user_id instead of generator id. Oh and the generator isn't returning me any value as well. There's suppose to be values return becoz i did call if params[:choice] == 'Randomly' @generator.random_generate(generator_params) if random is the selection – BC2 Dec 09 '13 at 10:20
  • 1
    okay, thanks for the reply! Have you tried my little fix I made: `user_generator_path(:user_id => current_user.id, :id => generator.id)`? – Richard Peck Dec 09 '13 at 10:25
  • thanks. it doesn't show any error in that line now but it's showing error for deleting. PReviously i haven't include a user for the generator so my code goes like this : `<%= link_to 'Delete', generator, method: :delete, data: { confirm: 'Are you sure?' } %>` but now i'm getting error `undefined method `generator_path' for #<#:0x59f7480>` – BC2 Dec 09 '13 at 10:33
  • It seems that your generator path is incorrect. `destroy` paths are the same as `show` paths, except they use the `delete` method – Richard Peck Dec 09 '13 at 10:35
  • 1
    Why don't you try this: `user_generator_path(:user_id => current_user.id, :id => generator.id), :method => :delete` ? – Richard Peck Dec 09 '13 at 10:35
  • Thanks. I didn't know delete == show. Thank you so much =). But if i may ask 1 more favor, my generator isn't calling the function. As u can see above, the create function will call the generator method if i choose 1 out of the 3 methods. ` if params[:choice] == 'Randomly' @generator.random_generate(generator_params) elsif params[:choice] == 'Specified ATGC' @generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C]) elsif params[:choice] == 'Seating' @generator.seating(params[:user_seq]) end` but my program isn't calling – BC2 Dec 09 '13 at 10:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42780/discussion-between-bc2-and-rich-peck) – BC2 Dec 09 '13 at 10:54