I'm implementing a custom update function called update_status for my users controller. I need some help with the routing. what I want to do is update a status that only admins can access. I'm calling the update function via a form helper through the edit function in the users controller. This is my code for the form helper:
<%= form_for #user, :url => url_for(:controller => "users", :action => "update_status"), method: :put do |f| %>
<%= render "shared/error_messages", object: f.object %>
<%= f.check_box :admin %>
<%= f.label :admin %>
<%= f.check_box :editor %>
<%= f.label :editor %>
<%= f.submit "Save Changes", class: "btn btn-primary" %>
<% end %>
But when I click save changes all I get is this error
I want to route the action so that the user id can be resolved.
Controller action code:
def update_status
if #user.update_attributes(status_params)
flash[:success] = "User updated"
redirect_to #user
else
render 'edit'
end
end
Routes:
Transpub::Application.routes.draw do
resources :users do
member do
put 'update_status'
end
end
resources :papers
resources :comments
resources :reviews
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
resources :subject_field, only: [:create, :destroy]
#get "users/new"
root "static_pages#home"
match "/signup", to: "users#new", via: "get"
match "/signin", to: "sessions#new", via: "get"
match "/signout", to: "sessions#destroy", via: "delete"
match "/help", to: "static_pages#help", via: "get"
match "/about", to: "static_pages#about", via: "get"
match "/contact", to: "static_pages#contact", via: "get"
match "/search_papers", to: "papers#index", via: "get"
match "/browse_papers", to: "papers#browse", via: "get"
In your routes files, look for the part that corresponds to the users controller and make sure that you have the following code
resources :users do
put :update_status, on: :member
end
That will declare the route. Another thing you have to update is the url of the form. Change the url to
form_for #user, :url => [:update_status, #user], html: { method: :put } do |f|
Related
I was struggling the whole day to make my button_to working. This is the code in my order index view:
<%= button_to("Remove", {:controller => "orders", :action => "update", :id => product.id}, :method => :patch) %>
my controller :
def update
#order.products.delete(Product.find(#product.id))
#product.ordinable = true
#product.save
#order.amount = 0
#order.save
#order_amountnew = #order.amount
#order.products.each do |x|
#order_amountnew = #order_amountnew + x.price
end
#order.amount = #order_amountnew
#order.save
if #order.products.empty?
#order.destroy
end
redirect_to orders_url
end
and my routes:
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users
resources :products
resources :orders, only: [:show, :create, :index, :destroy, :update]
post '/payment', action: :payorder, controller: 'orders'
patch '/orders', action: :update, controller: 'orders'
require "sidekiq/web"
authenticate :user, lambda { |u| u.admin } do
mount Sidekiq::Web => '/sidekiq'
end
end
I was trying to trigger the code inside the update method using the button_helper inside my order index. I come up with this solution and unexpectedly my button_to method now trigger the update method, but only after i spent hours googling and added to my routes this line of code:
patch '/orders', action: :update, controller: 'orders'
My question is now, whenever i want to trigger a method with a link_to or a button_to, i must create a route for each method like that right? Otherwise, i get routing error right? I have this doubt because here:
resources :orders, only: [:show, :create, :index, :destroy, :update]
i already defined an update route for my orders resource, so i don't understand why i need to specify again the route for the crud method i want to use. Thank you.
resources :orders, only: [:show, :create, :index, :destroy, :update] will yield update url as /orders/:id. You can verify that by poking in rake routes output.
patch '/orders', action: :update, controller: 'orders' yields update url as /orders.
The reason why latter works is because <%= button_to("Remove", {:controller => "orders", :action => "update", :id => product.id}, :method => :patch) %> send a request to /orders?id=.... You can verify that in the stacktrace
If you want to use 1, you should change button_to to format like <%= button_to("Remove", product_path(product), :method => :patch) %>. See https://api.rubyonrails.org/v5.2.1/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to for references.
Hope that helps :).
So I have a Message model, and a ChatRoom model.
When I display a chat room, I use the show action on the ChatRoom controller. In the view for this action, there is a little form for the user to create a post, and submit that post to the chat room being shown.
When I run my tests, however, I get an error "no route matches [POST] /messages/an_id_of_some_sort". Specifically, in this little test:
post message_path, params: {message: {body: "yo ho ho and a bottle of rum!"}}
assert_redirected_to chat_room_path(#channel)
the error pops up in the post message_path.
The show method on the chat room controller looks like
def show
if(#user = current_user)
#chats = #user.chat_rooms
#chosen = ChatRoom.find_by(id: params[:id])
if(#chosen.messages.any?)
#messages = #chosen.messages
else
#messages = nil
end
#message = Message.new
end
end
Then the little form bit of the view is:
<div class="message-input">
<%= form_for(#message) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :body, placeholder: "Write Message..." %>
<%= f.hidden_field :room, :value => params[:room] %>
<%= button_tag(type: "submit", class: "message-submit-btn", name: "commit", value: "") do %>
<span class="glyphicon glyphicon-menu-right"></span>
<% end %>
<% end %>
</div>
I have a create action on the Messages Controller which does the saving to the database:
#message = current_user.messages.build(message_params);
#message.chat_room = params[:room]
if #message.save
redirect_to chat_room_path(#message.chat_room)
end
and routing-wise I have
Rails.application.routes.draw do
root 'welcome#welcome'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get 'users/signup_success'
delete '/chat_rooms/leave/:id', to: 'chat_rooms#leave', as: 'current'
get 'welcome/welcome'
resources :users
resources :account_activations, only: [:edit] #Only providing an Edit route for this resource.
resources :password_resets, only: [:new, :edit, :create, :update]
resources :chat_rooms, only: [:new, :create, :show, :index]
resources :messages, only: [:create, :edit, :destroy]
end
I have tried playing around with explicitly setting the :url on the form_for, but no dice. There is another question on this problem, but the solution there hasn't really helped.
I would greatly appreciate any help.
With this line you're running POST /messages/:id
post message_path, params: {message: {body: "yo ho ho and a bottle of rum!"}}
In your routes file you have this:
resources :messages, only: [:create, :edit, :destroy]
This will create the routes POST /messages, PUT/PATCH /messages/:id, and DELETE /messages/:id. You can verify this with rake routes.
None of these generated routes handle POST /messages/:id.
If you're trying to have the test create a new message, then you can use messages_path instead. message_path (with singular message) takes a message argument as a message, e.g. message_path(Message.first) and uses that to build the url.
This code is all based on the Ruby on Rails tutorial by Michael Hartl. I created an assigned action in my users_controller with
def assign
puts "in assign..."
#scout = User.find(params[:follower_id])
#der = User.find(params[:followed_id])
end
I realize this doesn't do anything currently, but in the _follow partial, I have
<%= form_for #user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, #user.id %></div>
<%= f.label :followed_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
But I'm getting the error No route matches {:action=>"assign", :controller=>"users", :id=>"4"}. I'm new to rails so this could just be a stupid question. What am I doing wrong? Do I need to modify my routes.rb file? Also, if I tried <%= form_for #user do |f| %>, how does the controller know which action to use? Is it based on the action displaying the form?
Edit: my routes.rb file is
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
Edit 2: the HTML generated by #Rich's form_for block is
<form class="edit_user" id="edit_user_5" action="/users/5/assign" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="...">
<label for="user_followed_id">Assign Mr. Marley Muller to</label>
<select name="user[following]" id="user_following">
<option value="1">Example User</option>
<option value="2">Matthew Swartz</option>
<option value="3">Joseph Swartz</option>
</select>
<input type="submit" name="commit" value="Assign" class="btn btn-primary">
</form>
Which makes sense why I'm currently getting a can't find id error since it's not sending a real id (edit_user_5)
Edit 3: Here are the parameters being passed for the request
Started PATCH "/users/6/assign" for 68.100.59.128 at 2015-12-15 03:54:39 +0000
Processing by UsersController#assign as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"following"=>"2"}, "commit"=>"Assign", "id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Unpermitted parameter: following
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/users_controller.rb:69:in `assign'
Edit 4: With the following routes file,
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
and the following form for assigning one user to follow another,
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
I am getting the ActiveRecord::RecordNotFound in UsersController#assign error of Couldn't find User with 'id'=, but with the following parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"user"=>{"following"=>"3"},
"commit"=>"Assign",
"id"=>"4"}
The ID's are correct: "user"=>{"following"=>"3"} and "id"=>"4", I believe I am just trying to access them incorrectly. This is the assigns action in the users controller:
def assign
#scout = User.find(params[:id])
#der = User.find(params_hash[:followed_id])
# make scout follow der here
redirect_to #scout
end
Thoughts?
In your routes.rb file you have to define the route for the specific controller action like that:
resource :users do
member do
post 'assign' # change the request method to 'put'
end
end
It will then generate a route for your assign method in the controller like: users/4/assign
For more info visit guides
To add to Emu's answer, the code you have won't work...
def assign
puts "in assign..."
#scout = User.find(params[:follower_id])
#der = User.find(params[:followed_id])
end
The params you'd get from the form_for object would be structured as:
params[:user][:follower_id]
params[:user][:followed_id]
You'd have to use the following:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def assign
#scout = User.find params[:id]
#der = User.find params_hash[:followed_id]
end
private
def params_hash
params.require(:user).permit(:followed_id)
end
end
You could also get rid of the hidden field in your form if you're using the same #user object to populate it:
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :followed_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
Update
You need to include :assign in your routes:
Rails.application.routes.draw do
root 'static_pages#home'
resources :static_pages, only: [], path: "" do
collection do
get :help, :about, :contact
end
end
resources :sessions, only: [:new, :create, :destroy], path_names: {new: "login", create: "login", destroy: "logout"}
resources :users, path_names: { new: "signup" } do
member do
get :following, :followers, :assign
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
It turns out it was a combination of Emu and Rich's answers. The form in the HTML is
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
With controller action
def assign
#scout = User.find(params[:id])
#der = User.find(params[:user][:following])
#scout.follow(#der)
redirect_to #scout
end
and routes as
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)
My error says that:
Couldn't find Question with 'id'=your_questions"
and
ActiveRecord::RecordNotFound in QuestionsController#show
What should I do to fix it?
def show
#question = Question.find(params[:id])
#answer = Answer.new
end
on the second line it says where the error is.
Edit:
The Index View File
<%= form_for(#question) do |f| %>
<%= render 'common/form_errors', object: #question %>
<p>
<%= f.label :body, "Question" %><br />
<%= f.text_field :body %>
<%= f.submit "Ask a Question" %>
</p>
<% end %>
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
get '/questions/:id', to: 'questions#your_questions'
get '/search', to: 'questions#search'
You mention you have this route:
get '/questions/your_questions', to: 'questions#your_questions
If you also have a route like following the restful style, you should also have something like:
get 'questions/:id', to: 'questions#your_questions'
Or a resource call. Anyway, so Rails is actually trying to access your show action passing "your_questions" as the id for the route. Write this route like this:
get '/questions/:id', to: 'questions#show
This means: "If a request using the GET HTTP method follows the url 'questions/:id', then go to controller: questions and its action(method in the controller) called: your_questions" and pass into the params hash the value of id as in the URL.