0

I have two hidden_fields, user_id and skill_id

<%= form_for @skill do |s| %>
  <%= s.label :image, "Upload your skill" %>
  <%= s.hidden_field :user_id, value: current_user.id %>
  <%= s.hidden_field :skill_id, value: params[:id] %>
  <%= s.file_field :image, multiple: true %>
<% end %>

In my controller I have this:

def reviews
  @skill = Skill.new
end  

I'm able to get the value for skill_id into my database, but I'm not able to get the value from user_id. In my rails console, I see that user_id is being passed through "skill", but doesn't show in my database.

Parameters: {"utf8"=>"✓", "authenticity_token"=>"5+wxS929uxtt..", "skill"=>{"user_id"=>"7", "skill_id"=>"132", ...

I even checked to see if I'm getting any value with <%= current_user.id %>, which I am.

Maybe someone can guide me to the right path in debugging this issue.

Thanks

11
  • Does your HTML shows the user ID correctly? Is the form field named correctly on the generated HTML? Commented Feb 15, 2014 at 23:53
  • Its showing as "user_id"=>"" in your params that is why it is not getting passed to database. Commented Feb 15, 2014 at 23:56
  • Is your user_id set in the attr_accessible? Also I am assuming that a skill belongs to a user. Why not just use @skill=current_user.skills.new in your controller? Commented Feb 16, 2014 at 0:02
  • @fotanus yes its showing correctly and its show value too Commented Feb 16, 2014 at 0:14
  • @KirtiThorat sorry it was a typo. it shows a value id when I checked Commented Feb 16, 2014 at 0:14

2 Answers 2

1
<%= form_for @skill do |s| %>
   <%= s.label :image, "Upload your skill" %>
   <%= s.hidden_field :user_id, value: current_user.id %>
   <%= s.hidden_field :skill_id, value: params[:id] %>
   <%= s.file_field :image, multiple: true %>
<% end %>

This will go to the create action

So what you really want is to update an existing one since you are passing an existing skills id

def reviews
  @skill = Skill.find params[:id]
end

Now your form can use this

<%= form_for @skill do |s| %>
   <%= s.label :image, "Upload your skill" %>
   <%= s.hidden_field :user_id, value: current_user.id %>
   <%= s.file_field :image, multiple: true %>
<% end %>

It is now go to the update action, in your controller.

def update
  @skill = Skill.find params[:id]
  @skill.update_attributes params[:skill]
  redirect_to root_path # redirect to somewhere
end

Try this.

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

5 Comments

I don't think you're understand my questions
Your problem is that you cannot get user_id into the database. You can get the user_id from the way above and then save it to your @skill. and do you have any line like this Skill.create params[:skill]
no, I have only .new. If I do .create, then my other fields don't get inputed unless I put it into create(params...)
Thanks, this and @Sean's answer got me in the right direction
Ok I updated my answer to have some kind of save in the database
1

You need a create controller that would look something like this:

def create
  @skill = Skill.new(params[:skill])
  if @skill.save
    # Handle a successful save.
  else
    render 'new'
  end
end

You may want to change the name of the def reviews controller to def new, and also add the new routes to config/routes.rb

1 Comment

this actually got me looking in the right direction... thanks

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.