开发者

how to send a parameter not associated with the model object using form_for in rails

开发者 https://www.devze.com 2023-01-08 13:56 出处:网络
I use form_for to save a model object site <% form_for :site :url => {:controller => \'site\', :action => \'add_site\' } do |f| -%>

I use form_for to save a model object site

<% form_for :site :url => {:controller => 'site', :action => 'add_site' } do |f| -%>

       开发者_如何学Go   <%= f.text_field :protocol, :size => 127, :style => 'width:255px' , :value => "http://"%>
          <%= f.text_field :site_name, :size => 127, :style => 'width:255px' , :value => "www."%>

        <%= f.hidden_field :user_id, :value => @user.id %>
        <%= f.hidden_field :status, :value => 'Not Verified' %>

<% end -%>

here the field protocol is not a instance of the model site. but i just want to pass to the action add_site so that i can use as params[:protocol]

what should i do to achieve this?


You can set something like this:

<%= text_field_tag :protocol %>

To the action of the controller you can refer to this as params[:protocol]


Add it to your site model as an attribute accessor:

attr_accessor :protocol

See http://railscasts.com/episodes/16-virtual-attributes .

0

精彩评论

暂无评论...
验证码 换一张
取 消