Reputation:
I'm trying to insert the input from a select_tag to my controller method. I've looked and cannot seem to resolve the issue.
This is the code I have below, nothing for the rank's selection comes up in the params at all.
<h1>hello please set <%= @user.username %>'s rank'</h1>
<%= select_tag 'rank', options_for_select(@ranks.collect{ |r| [r.rank_name] }) %>
<%= button_to "Update", :action => "set_user_rank_update", value: "#{@user.id}", method: :post %>
Update below with the controller and routes
Controller:
class Admin::RankController < ApplicationController
before_action :admin?
def new
@rank = Rank.new
end
def create
@rank = Rank.new(rank_params)
if params["rank"]["admin"].to_i == 1
@rank.toggle! :admin?
end
if @rank.save
flash[:success] = "Rank created"
redirect_to root_path
else
flash[:danger] = "Failed to create rank"
render 'new'
end
end
def set_user_rank_new
@user = User.find_by_id(params["format"])
@ranks = Rank.all
end
def set_user_rank_update
@user = User.find_by_id(params["value"])
@rank = Rank.find_by_id(params["rank"])
@rank_backup = @user.rank.first
debugger
@user.rank - @user.rank.first
@user.rank << @rank
if @user.rank.first == @rank
flash[:success] = "Set user's rank"
redirect_to root_path
else
flash[:danger] = "Failed to set user's rank"
@user.rank - @user.rank.first
@user.rank << @rank_backup
render 'set_user_rank_new'
end
end
private
def rank_params
params.require(:rank).permit(:rank_name, :rank_color)
end
end
Routes
Rails.application.routes.draw do
devise_for :users,
:controllers => { :registrations => "member/registrations" , :sessions => "member/sessions"}
scope module: 'public' do
root 'welcome#index'
end
scope module: 'member' do
get 'members/:id' => 'member#show'
end
scope module: 'admin' do
get 'rank/new' => 'rank#new'
post 'rank/create' => 'rank#create'
get 'rank/set_user_rank/new' => 'rank#set_user_rank_new'
post 'rank/set_user_rank/update' => 'rank#set_user_rank_update'
end
end
Upvotes: 1
Views: 50
Reputation: 513
For what purpose do you need it in your controller action? With the link_to you can forwards params as well. It would be very helpful to see your controller and also your routes.
Upvotes: 0
Reputation: 75
The button_to
helper creates an inline form with just the parameters included in the helper statement (in this case the user id).
You can check this by examining the resulting HTML on your page - which I think will show an input field for rank sitting outside the form tag.
To include the rank parameter, you should set up the form using a form helper and make sure the rank input is included inside the form.
Upvotes: 1
Reputation: 470
Try passing 2 element arrays to options_for_select. The way you have it looks like it would get you option text but no values, which could explain why it doesn't show up in the params.
So for example:
<%= select_tag 'rank', options_for_select(@ranks.collect{ |r|[r.rank_name, r.id] }) %>
Upvotes: 1