how to pass id in controller - ruby-on-rails-3

I have this error . I'm using users using Omniauth-identity and Omniauth-FB TWITTER from railscast video.
No route matches [GET] "/users/generators/new"
when i click
<%= link_to 'GENERATE RAPD PRIMER',new_user_generator_path(#user), id:'new' %></li>
What this route does in rake routes is
new_user_generator GET /users/:user_id/generators/new(.:format) generators#new
When i click
<%= link_to 'GENERATE RAPD PRIMER',user_generator_results_path(#user), id:'new' %></li>
my URL returns
http://localhost:3000/users//generators/new
There's no id return in that URL. How come ??? How can i fix it? I'm using railscast tutorial about Omniauth FB and Identity for User model. I'm trying to associate User with generator.

Make sure you pass user_id param as per routes:
new_user_generator_path(user_id: #user.id)

Related

Routing Error after scaffolding and migration

I created a scaffold without problems
$ rails generate scaffold New name:string title:string content:text
Rake command to run the migration (no problems as before, table correctly created)
$ rake db:migrate
Edit app/views/home/index.html.erb
<%= link_to 'My News', :controller => 'news' %>
I see the home and the link correctly in "http://localhost:3000"; clicking the link "My news" page "http://localhost:3000/news" is loaded without errors.
Now, clicking in the link "New New" generated by Rails, link's target localhost:3000/news/new (source "<a href ="/news/new">New New</ a>"), i read this error:
Routing Error
No route matches {:action=>"show", :controller=>"news", :format=>nil}
Try running rake routes for more information on available routes.
In "app/views/news/index.html.erb" the link souce is
<%= link_to 'New New', new_news_path %>
In routes.rb i read
MyApp::Application.routes.draw do
resources :news
get "home/index"
Rails 3.2.3
Ruby 1.9.3p125
MySQL 5.5
Windows 7 64 bit
Rakes routes:
news_index GET /news(.:format) news#index
POST /news(.:format) news#create
new_news GET /news/new(.:format) news#new
edit_news GET /news/:id/edit(.:format) news#edit
news GET /news/:id(.:format) news#show
PUT /news/:id(.:format) news#update
DELETE /news/:id(.:format) news#destroy
home_index GET /home/index(.:format) home#index
root / home#index
Thanks in advance and sorry for my English
you have to use news_index_path because news is not singular if rails can't make singular - prular distinguish they will add _index at the end :)
You have one news and many news and this is confusing.
Always try to use <name_of_resource>_path to generate urls :)
news_index GET /news(.:format) news#index
This says it implicit, you use 1 part news_index and add _path to get path for it.
You should have
<%= link_to 'My News', news_index_path %>
Hope that helps, cheers!

Using Devise, I don't see notices like "you've signed in" when i log into my app

I'm building a basic app with Rails 3.2 and Devise 2.0. I've create a User devise model and a Projects model. In my routes.rb files I have
root :to => 'projects#index'
I can sign up and sign in at Get users/sign_up and Get users/sign_in, respectively, but when it redirects to projects#index, I don't see a notice at the top that says "You've signed in successfully. Which file do I need to check to fix this?
You need to add flash messages showing to the file /projects/index.html.erb
for example this way:
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>

Create New User with info from another Model - Rails 3.0

I am using Rails 3 and Devise for user authentication. I created a separate scaffold, request_new_user, and I want to have a link on the index page for all of the people who requested an account to go to the new_user_path, with their information sent as well to populate the fields. How would I set the params so I can set the values within the user controller? Or is there a better way to do this? I mainly just want to pass the new user's name and email.
You can generate devise views in your project by: rails generate devise:views .
Send your params in GET request: /signup?email=...&name=...
In registration view you can apply your params, something like:
<%= f.input :email, :value => params[:email] %>
Hope it helps.

No route matches {:controller=>"results", :action=>"show"}

Im upgrading a rails 2 app to rails 3 app and am having a bunch of routing issues. Heres the current:
So in the page its trying to load (results/_form.html.erb) I have a form with the following syntax:
<%= form_tag(:controller => "results",:action => "show") do %>
And i do have an action in my results_controller.rb called show. Yet i keep getting the no route matches error. Is this rails 2 syntax and not 3? Is there something I need to do in my routes.rb file? I think there is because that was a a major change between rails 2 and 3, im just not sure what. Any suggestions?
show action ideally should expect id to be passed in the params:
<%= form_tag(:controller => "results",:action => "show", :id => #user.id) do %>

Rails 3 how to add a custom method to controller

I'm using the http://guides.rubyonrails.org/getting_started.html as an example to help me create my own application. I create the blog and comments modules just fine. When I add a method to the comments or blog controllers I cannot get a link_to action to work calling the new function. Everything points to a problem in the routes.rb but I've tried all the new syntax I've seen and nothing is working for me.
What I'm trying to do is create a simple execute method in the controller to run a ruby script and save the output to the database. Everything works according to the tutorial but when I try to extend the comment controller with a custom function called execute I cant get that to run.
comments_controller.rb #Same as destroy
def execute
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
_comment.html.erb
<%= link_to 'Execute Comment', [comment.post, comment],
:method => :execute %>
routes.rb
resources :posts do
resources :comments do
get :execute, :on => :member
end
end
rake routes |grep execute
execute_post_comment GET /posts/:post_id/comments/:id/execute(.:format) {:action=>"execute", :controller=>"comments"}
Error when I click Execute comment link:
No route matches "/posts/3/comments/6"
run rake routes and see if there are any routes pointing to your controller action. If not you'll need to create one either as a "member action" or with a match rule.
If you do see the route, you can name it by passing an :as => route_name parameter to the routing rule. Doing so will enable the route_name_path() and route_name_url() helpers for your link_to
RailsCasts has a good quick rundown of the rails 3 routing syntax here
EDIT:
based on the code examples, try this :
<%= link_to 'Execute Comment', execute_post_comment_path(comment.post, comment) %>
According to the docs here the :method option can only contain valid http verbs (get, put, post, delete). The link_to helper can't puzzle out which action you want to hit with a custom member action, so you have to use the named route as above.
HTH