Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

rails params permitted: false when updating

I have a site where uses have a profile and can add an art to their profile and the month_started, year_started, and rank in that art. I am able to add the art but I cannot get update to work. I'm doing it from within the users folder on the show page. I render the add_art form on the profile page and it submits to the user_arts_controller's create action. For edit_art I am trying to use the edit.html.erb page inside users and it pulls in the information from the @art_user table to populate the fields, but when I submit it acts like it's updating and goes back to the set page and gives a success message but nothing gets updated. I used byebug and it shows all the params but says permitted: false.

Here is my form:

<% vars = request.query_parameters %>
<% @add = vars['add'] %>
<% if @add == "1" %>
  <% art = "Judo" %>
<% else %>
  <% art = "Brazillian Jiu Jitsu" %>
<% end %>
<div class="container">
  <div class="row center form">
    <div class="col-sm-12 col-md-10">
      <div class="card-panel">
        <h3>Edit <%= art %> info.</h3>
        <%= form_with(model: @art, url: art_edit_path(art_id: @add), class: "shadow p-3 mb-3 rounded text-dark", local: true) do |f| %>
          <div class="form-group row">
            <div class="col-md-3 col-sm-12 col-form-label">
              <%= f.label :month_started  %>:<span class="text-danger">*</span>
            </div>
            <div class="col-md-9 col-sm-12">
              <%= f.select :month_started, Date::MONTHNAMES[1..12], {prompt: true}, {class: 'form-control'} %>
            </div>
          </div>

          <diva class="form-group row">
            <div class="col-md-3 col-sm-12 col-form-label">
              <%= f.label :year_started  %>:<span class="text-danger">*</span>
            </div>
            <div class="col-md-9 col-sm-12">
              <%= f.select :year_started, Date.today.year-70 .. Date.today.year, {prompt: true, order: [:year]}, {class: 'form-control'} %>
            </div>
          </diva>
  
          <% @rank = Rank.where(art_id: @add) %>
          <div class="form-group row">
            <div class="col-md-3 col-sm-12 col-form-label">
              <%= f.label :rank  %>:<span class="text-danger">*</span>
            </div>
            <div class="col-md-9 col-sm-12">
              <%= f.collection_select :rank_id, @rank, :id, :rank, {:prompt=>true}, {:class=>'form-control'} %>
            </div>
          </div>

          <div class="form-group row text-right">
            <div class="col-md-8 col-sm-12"></div>
            <div class="col-md-4 col-sm-12">
              <%= f.submit (@add == "1" ? "Edit Judo" : "Edit Brazilian Jiu Jitsu"), class: "profile_btn" %>
            </div>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</diva>

Here is the user_arts_controller.rb:

class UserArtsController < ApplicationController 
  def create 
    art_to_add = Art.find(params[:art_id])
    unless current_user.arts.include?(art_to_add)
      UserArt.create(art: art_to_add, user: current_user, month_started: params["month_started"], year_started: params["year_started"], rank_id: params["rank_id"])
      flash[:notice] = "You have successfully enrolled in #{art_to_add.art}"
      redirect_to ("/users/#{current_user.username}?cat=comp")
    else
      flash[:notice] = "Something went wrong!"
      redirect_to ("/users/#{current_user.username}")
    end
  end

  def edit
    @art = UserArt.find_by(user_id: current_user.id, art_id: 1)
    if @art
      @my_rank = Rank.find(id: @art.rank_id)
    end
  end

  def update
    byebug
    @user_art = UserArt.where(id: params[:id])
    
    if @user_art.update(art_params)
       flash[:notice] = "You have successfully updated your art"
       redirect_to ("/users/#{current_user.username}?cat=comp")
    else
      flash[:notice] = "Something went wrong!"
      redirect_to ("/users/#{current_user.username}")
     end
  end

  private
  
  def art_params 
    params.require(:user_arts).permit(:user_id, :art_id, :month_started, :year_started, :rank_id)
  end
end

Here are my routes:

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
  root 'pages#index'
  get 'events', to: 'pages#events'
  get 'features', to: 'pages#features'
  get 'pricing', to: 'pages#pricing'
  get 'about', to: 'pages#about'
  get 'contact', to: 'pages#contact'
  get 'privacy', to: 'pages#privacy'
  get 'terms', to: 'pages#terms'
  get 'remove', to: 'pages#remove'
  delete 'logout', to: 'sessions#destroy'
  resources :users, except: [:new], param: :username
  resources :clubs, param: :club_username
  post 'club_join', to: 'user_clubs#create'
  post 'art_add', to: 'user_arts#create'
  post 'art_edit', to: 'user_arts#edit'
  patch 'art_edit', to: 'user_arts#update'
end

Here is what I get when I use byebug and enter params:

(byebug) params
<ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"UY4RG1D0N0IHcJeyftn44WmAgsdslKJScD+GSJPbAUEEjlJLiYLd2htK4RBTfrP+YPUXP5EFB5pQc98OmQPPtg==", "user_art"=>{"month_started"=>"January", "year_started"=>"1988", "rank_id"=>"14"}, "commit"=>"Edit Judo", "art_id"=>"1", "controller"=>"user_arts", "action"=>"update"} permitted: false>
(byebug)

Any help is greatly appreciated.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you have an extra s in the definition of your allowed params. Since you are getting this:

"user_art"=>{"month_started"=>"January", "year_started"=>"1988", "rank_id"=>"14"}

You should check in this way:

def art_params 
  params.require(:user_art).permit(:user_id, :art_id, :month_started, :year_started, :rank_id)
end

Just a final comment. Take a look at the params you're permitting, I guess user_id shouldn't be one of them.

Let me know if it works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...