1

I am trying to render a new view on an already existing user show page. When trying to submit this view, I get param is missing or the value is empty: user. To be clear this is a skill partial being rendered on the user show page. For some reason it is using the strong params in my User Controller.

The code:

show.html.erb for user

<h4>Create a Skill</h4>
<%= render partial: "skills/form" %>

userscontroller.rb

def show
    @user = User.find(params[:id])
    @skill = Skill.new
    @skills = @user.skills.all
  end

  private

  def user_params
    params.require(:user).permit(:username, :password, :avatar_url, :email, :about, :cover_letter, :city, :state)
  end
end

SkillsController.rb

class SkillsController < ActionController::Base
  def new
    user = User.find(params[:user_id])
    @skill = user.skills.new
  end

  def create
    user = User.find(params[:user_id])
    @skill = user.skills.new(skill_params)
    if @skill.save
      flash[:message] = "#{@skill.name} skill has been created!"
      redirect_to user_path(user)
    else
      redirect_to new_user_skill_path
    end
  end

  private

  def skill_params
    params.require(:skill).permit(:name, :level)
  end
end

Also, I have Namespaced skills within user. No authentication in place yet.

EDIT: @nickm, here are the contents of skills/_form

<%= simple_form_for(Skill.new, :url => { :action => "create" })  do |f| %>
  <%= f.input :name, label: 'Skill Name ' %>
  <%= f.input :level, label: "Skill Level ", collection: ["Beginner","Proficient", "Intermediate", "Advanced", "Expert"], include_blank: false, include_hidden: false %>

  <%= f.submit %>
<% end %>
2
  • What's in skills/_form? Commented Sep 7, 2017 at 16:52
  • @NickM updated the OP. Commented Sep 7, 2017 at 16:54

1 Answer 1

1

The problem is that you aren't passing a user_id through the form. You would have to either add a form input:

<%= f.hidden_field :user_id, some_value %>

Then find the user:

user = User.find(params[:skill][:user_id])

and then make skill_params

def skill_params
  params.require(:skill).permit(:name, :level, user_id)
end

Or optionally, set the value of user_id in your controller action. Not sure how you're going to pass that value since you haven't built any authentication yet. If you were using something like devise you could do

current_user.skills.new(skills_params)

...in your create action.

Sign up to request clarification or add additional context in comments.

1 Comment

Incredible. I learned about hidden actions but haven't had a chance to use them yet. Thank you, I'm going to implement authentication now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.