How to set a predefined value in a form in Rails

Posted by marco on Stack Overflow See other posts from Stack Overflow or by marco
Published on 2010-05-25T19:07:57Z Indexed on 2010/05/25 19:11 UTC
Read the original article Hit count: 181

Filed under:
|

So I just got started in Rails, and I'm trying to create an Object (a book_loan in my case) with a Form. The thing is that I get to this form by clicking on a book, so I pass the book_id as a parameter, like localhost:3000/loans/new?id=1.

Now I don't want the user to be able to set the book id field in the form, since I already know the id. So my question is how to set the value in the form. I have been trying things like:

<% form_for(@loan) do |f| %>
  <%= f.error_messages %>
  ...
  <%= @loan.book_id = params[:id] %>
  <%= f.submit 'Create' %>
<% end %>

without any success. Does anybody have a hint for me?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about beginner