Can't link to another page with user - ruby-on-rails

I want link to another page with my user, but i have an error:
RuntimeError in WelcomeController#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
In my current page
<%= link_to "edit", welcome_edit_path(#user) %>
thi is my controller
def edit
user = User.find_by_id(#user.id)
end
in my page i want link to
<%= user.id %>
I know my problem is my #user was nil, but i dont know how to link to another page with my #user
So, please! help me
this my routes file
get "micropost/new"
get "user/new"
get "user/saved"
get "post/new"
get "post/show"
get "welcome/index"
get "welcome/sucess"
get "welcome/edit"
root :to => "welcome#index"
get '/users/:id', :to => 'welcome#sucess', :as => "user"
match '/relations', to: 'relation#create', via: 'post'
match '/relations/:id', to: 'relation#destroy', via: 'delete'
resources :users
resources :relations, only: [:create, :destroy]
resources :microposts
match '/login', to: 'welcome#create', via: 'post'
match '/logout' => 'welcome#destroy', as: :logout
match '/create', to: 'micropost#create', via: 'post'
match '/signup', to: 'user#signup', via: 'post'

Your code in edit method makes no sense. First thing, you have to define your route properly:
get '/welcome/edit/:id', to: 'welcome#edit', as: 'welcome_edit'
Then, in the WelcomeController:
def edit
#user = User.find(params[:id])
end
and the link to this page:
<%= link_to 'edit', welcome_edit_path(#user) %>

Related

Strange locale issue in the Rails

I have a url
<li><%= link_to "#{t('feed.add')}", feed_path(locale) %></li>
And the problem is, that after the registration, when I press this link for the first time it redirects me to the mydomain.com/feed and prints the next error:
"feed" is not a valid locale
in the
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
But when I return back and clicks again it redirects me to the:
mydomain.com/en/feed
So, what is the problem of this strange problem?
UPDATE
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
match '', :via => [:get], to: redirect("/#{I18n.locale}")
get '/en/youarethechampion' => "winners#winner"
get "/en/hook" => "feed#hook"
get '/thisvideonotfound' => 'feed#notFound'
get '/:locale/feed', to: 'feed#index', as: 'feed'
post '/:locale/feed/create', to: "feed#create"
get '/:locale/feed/new', to: "feed#new", as: 'feed_new'
get '/:locale/feed/destroy', to: "feed#destroy", as: 'feed_destroy'
get '/:locale/feed/edit', to: "feed#edit", as: 'feed_edit'
get '/:locale/feed/update', to: "feed#update", as: 'feed_update'
scope :path => "(:locale)", :locale => /en|ru/ do
devise_for :users
end
match '/:locale/posts/search/', to: 'home#search', as: 'search', via: :get
get '/:locale/page/:page_slug' => 'home#about'
get '/:locale/:category/:id/', to: 'home#show', as: 'feed_show'
get '/:locale/:id/', except: 'search', to: 'home#by_category', as: 'category'
get '/:locale/posts/user/:name/', to: 'home#by_user', as: 'user'
get '/:locale/posts/tag/:tag/', to: 'home#by_tag', as: 'tag'
get '/:locale/pages/:page/', to: 'page#show', as: 'page'
post '/feed/:id/comments/', to: 'home#create_comment'
get '/:locale/', to: "home#index"
Ok, looks like scenario after the registration: locale is nil, and this line
<li><%= link_to "#{t('feed.add')}", feed_path(locale) %></li>
make link to just /feed. (Try it print it in front of the link)
<li>
Locale is <%= locale %>
<%= link_to "#{t('feed.add')}", feed_path(locale) %>
</li>
Then routes think that /feed is belongs to this route
get '/:locale/', to: "home#index"
and it tries to set feed as the locale. It doesn't work! Then you move to another page you define "real" local through default_locale -> en.
I18n.locale = params[:locale] || I18n.default_locale

how to remove action and controller from url in rails link_to

Here is my link_to
<%= link_to sub_category.name, controller: :posts, action: :product, id: "#{sub_category.slug}-#{sub_category.id}" %>
Which is pointing to the url
http://localhost:3000/posts/product/fifith-category-s-sub-category-2
I need the url as follows
http://localhost:3000/fifith-category-s-sub-category-2
How can i do it.
my route.rb
resources :posts
match ':controller(/:action(/:id))(.:format)', via: [:get,:post]
what #MarekLipka suggests is correct but defining your routes like this will take up all the single level namespace in your app i.e. "/anything" will route by default to posts#product.
I recommended using some form of identifier to figure out which routes should go to posts#product. What will work for you depends on why you want to do this. Couple of options are:
Use a short namespace:
scope '/pp' do
get ':id', to: 'posts#product
end
# "/pp/:id" routes to 'posts/product'
# pp is a random short name I picked, it could be anything
# link
<%= link_to sub_category.name, "pp/#{sub_category.slug}-#{sub_category.id}" %>
Use a constraint:
get ':id', to: 'posts#product`, constraints: { :id => /sub\-category/ }
# only id's with 'sub-cateogry' route to 'posts/product'
# link (assuming that sub_category.slug has 'sub-category' words in it)
<%= link_to sub_category.name, "#{sub_category.slug}-#{sub_category.id}" %>
If you want path /:id match your posts#product, you should have something like this in your routes:
resources :posts
match ':id', to: 'posts#product`, via: :get
match ':controller(/:action(/:id))(.:format)', via: [:get, :post]

Routing Error: uninitialized constant UserFriendshipsController

I am working on a Team Treehouse Ruby on Rails app that emulates a basic Facebook app.
I am now added social feature's like friends.
I recently added the following link to one of my views:
<%= link_to "Add Friend", new_user_friendship_path(friend_id: #user), class: 'btn' %>
When click the resulting button, I get the following error:
Routing Error
uninitialized constant UserFriendshipsController
Try running rake routes for more information on available routes.
I am thinking the problem is in either my "user_friendship_controller.rb" of "config/routes.rb " file.
Here is my "user_friendship_controller.rb file:
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
if params[:friend_id]
#friend = User.where(profile_name: params[:friend_id]).first
#user_friendship = current_user.user_friendships.new(friend: #friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
end
And here is my "config/routes" file:
Treebook::Application.routes.draw do
as :user do
get '/register', to: 'devise/registrations#new',via: :get, as: :register
get '/login', to: 'devise/sessions#new', via: :get, as: :login
get '/logout', to: 'devise/sessions#destroy', via: :delete, as: :logout
end
devise_for :users, :skip => [:sessions]
as :user do
get '/login' => 'devise/sessions#new', as: :new_user_session
post '/login' => 'devise/sessions#create', as: :user_session
delete '/logout' => 'devise/sessions#destroy', as: :destroy_user_session
end
resources :user_friendships do
end
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
get '/:id', to: 'profiles#show', as: 'profile'
end
Any help figuring out this bug is a great help.
Thanks
It looks like it's just a simple mis-naming of your file.
Try renaming it to user_friendships_controller.rb. Rails expects your class declaration to match the file name (as it stands it'd be looking for you to define UserFriendshipController).

ruby on rails button_to not activating the delete method

Hi im following the agile web development ebook and i cant seem to activate the logout action
here are the revelant parts (TAB key not working could not format to code)
rake routes
logout DELETE /logout(.:format) sessions#destroy
from the route file
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
my controller
def destroy
session[:user_id] = user.id
redirect_to store_url , notice: "Logged out"
end
and my view (relevant part)
<%= button_to 'Logout', logout_path, method: :delete %>
the error message is
No route matches [GET] "/logout"
i know it should use delete method but nothing i do seems to help
You may need to add a match in your routes. Sorry that I don't have the book with me to refer to.
Put this above your controller :sessions ...
match 'logout' => 'sessions#destroy', :as => :logout
If you didn't put the above line, your logout path should be sessions_logout_path, not logout_path.
Reference:
http://guides.rubyonrails.org/routing.html#naming-routes
match '/logout' => 'sessions#destroy', :via => :delete
or
controller :sessions do
member do
delete :destroy, :as => :logout
end
end

Form Sending To The Wrong PUT Request

I have two models for users ; the Plans model has_many users. Now, what I would like to do is to allow users to upgrade/downgrade their plans, by changing the plan_id. I've set up a form, as well as the appropriate action, but when I hit submit, it doesn't seem to do what the PUT action says. It seems to use the update action.
Here's my form :
<%= form_tag("/users/update_plan", :method => "put" ) do %>
<%= hidden_field_tag :plan_id, plan.id %>
<%= submit_tag("Change To Plan", :class => "signup") %>
<% end %>
Here's my update action
def update_plan
#user = current_user
#user.plan_id = params[:plan_id]
#user.save
sign_in #user
redirect_to change_plan
end
When I submit the form above though, it not only doesn't register the change, but I think it uses the update action, and not the update_plan action. The reason I think this is because it redirects to the what's in the update action, and it flashes the same thing as the update action.
def update
#user = current_user
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in #user
redirect_to edit_user_path(#user)
else
render 'edit'
end
end
Here's my routes.rb file
Dentist::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :phones, only: [:new, :create, :destroy]
resources :find_numbers, only: [:new, :create, :destroy]
put 'users/update_plan'
match '/signup', to: 'users#new'
match '/login', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/change_plan', to: 'users#change_plan'
root to: 'static_pages#home'
match '/product_demo', to: 'static_pages#product_demo'
match '/pricing', to: 'plans#index'
match '/contact', to: 'static_pages#contact'
And here's a console screenshot of what's happening:
http://stepanp.com/debug3.jpg
It seems to say it's using the update_plan action, but... :S
Any help on trying to get Update_plan action to function would be greatly appreciated!
The form is going to the right place (/users/update_plan), but that is being routed to:
UsersController#update
as it says on the second line of your console log. So not the action you expect, and the problem is in your routes. Try this to list all your routes:
rake routes
Probably the users update route (created by resources :users) is catching this first:
PUT /users/:id(.:format) users#update
There are no restrictions on the content of id, and format is optional, so users/update_plan would call users/update with an id of update_plan (in fact you can see that is happening at the edge of your console log screenshot, look for the :id => parameter).
So I would move your custom route to the top of the routes file first above resources :users, and also try change it to direct to the action you want, not sure what a route with no action specified does...
put '/users/update_plan', to: 'users#update_plan'

Resources