<% if current_user.blank? %>
<%tripowner=User.find_by_id(@trip.userid).id%>
<%=tripowner%>
<h3 class="text-center">Please sign up to provide recommendations.</h3>
<%= form_tag usersback_url, class: "form-signin", method: 'post' do %>
<h2 class="form-signin-heading">SIGN UP</h2>
<div>
<%= text_field_tag :first_name, nil, class: "input-block-level", placeholder: "First" %>
</div>
<div>
<%= text_field_tag :last_name, nil, class: "input-block-level", placeholder: "Last" %>
</div>
<div>
<%= email_field_tag :email, nil, class: "input-block-level", placeholder: "Email address" %>
</div>
<div>
<%= password_field_tag :password, nil, class: "input-block-level", placeholder: "Password" %>
</div>
<div>
<%= password_field_tag :password_confirmation, nil, class: "input-block-level", placeholder: "Confirm Password" %>
</div>
<%= hidden_field_tag :tripowner, value: tripowner %>
<div>
<%= submit_tag "SIGN UP", :class => 'btn btn-large btn-success' %>
</div>
<% end %>
<%end%>
:tripowner
未正确通过参数,但返回 nil。我正试图在行动中做到这一点。我已经确认tripowner
收益率为 1。因此,我想@friendship.f2
成为 1。它在下面返回 nil 是否有原因?
def createback
@user = User.new
@user.first_name = params[:first_name]
@user.last_name = params[:last_name]
@user.email = params[:email]
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]
@friendship = Friend.new
@friendship.f2 = params[:tripowner]
if @user.save
@friendship.f1 = @user.id
@friendship.confirmed = true
@friendship.save
reset_session
session[:user_id] = @user.id
redirect_to :back, notice: "Signed up successfully."
else
render 'new'
end
结尾
朋友模型
class Friend < ActiveRecord::Base
belongs_to :user
end
params: 参数:{"utf8"=>"✓", "authenticity_token"=>"BkVY0/z0KeCQRg83wvumUKEmT67R4liMZr33u98mTBE=", "first_name"=>"rki", "last_name"=>"rki", "email"=>" test@k", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "tripowner"=>"{:value=>1}", "commit"=>"注册"}
如您所见, 1 在tripowner 中被正确传递。