1
votes
<% 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 is not passing through the params correctly, but is returning nil. I am trying to do this with it in the action. I have confirmed that tripowner yields 1. Therefore, I want @friendship.f2 to be 1. Is there a reason why it's returning nil below?

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

end

friend model

class Friend < ActiveRecord::Base
belongs_to :user
end

params: Parameters: {"utf8"=>"✓", "authenticity_token"=>"BkVY0/z0KeCQRg83wvumUKEmT67R4liMZr33u98mTBE=", "first_name"=>"rki", "last_name"=>"rki", "email"=>"test@k", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "tripowner"=>"{:value=>1}", "commit"=>"SIGN UP"}

As you can see, the 1 is being passed properly in tripowner.

2
You should pass the id of the user and re-retrieve it in the controller. That said, doing DB lookups in the view is generally a bad idea--pass it in instead.Dave Newton
the variable tripowner returns the id. note the .id at the end of it's definition.brad
Oh, sorry, missed that-one reason to use whitespace, btw. Still, if you have a form, the value will be under the form's params, not at the root level--you can determine where it is by actually looking at the params being sent in. If it's not being sent in, or it's missing, then as Manoj said you'll need to set the value.Dave Newton

2 Answers

0
votes

Try this:

<%= hidden_field_tag :tripowner, value: tripowner %>

I think you should use tripowner_id instead of tripowner just to follow Rails' conventions, so it can be something like:

<%= hidden_field_tag :tripowner, value: User.find_by_id(@trip.userid).id %>
0
votes

You should set up associations so that you can access the trip owner via association, rather than having to run a separate query for it.

In this case a trip should belong to a user, and a user should have many trips...

   Trip.rb  # model
   belongs_to :user

   User.rb  # model
   has_many :trips

With this in place you can access the trip owner via:

   @trip.user.id 

Assuming your acting on a @trip, you shouldn't really even need to pass anything through a hidden field. Otherwise specifying something like this should work:

  <%= hidden_field_tag :tripowner, @trip.user.id %>

== Update

With the new code you posted, you should be using form_for @user... This will correctly namespace all the attributes in the form with user: {attributes}. With this in place instead of individually assigning your model attributes you can do something like this in your controller action:

  @user = User.new(params[:user])

It would also be helpful if you dig into your logs and paste the params hash thats being sent to your controller action. If your running this locally, it would be in log/development.log.

== Second Update

First check to make sure the params hash being sent to your controller looks correct. If it does, its likely an issue with your Friendship model.

Docs on hidden_field_tag: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag