1
votes

Fairly simple question, and I've seen lots of similar ones asked in my search, but none of the solutions I've read have worked so far. Not sure where I'm going wrong.

In my application I have Projects and Verifications. When viewing a particular Project, I need to be able to have a link to "Verify Project", which will send the user to a new Verification form. They will enter a variety of values, but I do not want them to have to select the Project that this Verification belongs to by hand -- I want to be able to pass the project_id directly alongside the values the user enters in the Verification form.

On my project view I have:

<%= link_to "Verify Project", new_verification_path(:project_id => @project.id ) %>

and then in the verifications controller:

def new
    @verification = Verification.new(params[:verification])
    @project = params[:project_id]
end

  def create
    @verification = Verification.new(params[:verification])
    @verification.project = @project
end

but this yields this error:

Validation failed: Project does not exist, Project does not exist

How can I create my Verification with :project_id being grabbed from the previous page?

EDIT

From the Rails log: (clicking Verify Project)

Started GET "/verifications/new?project_id=4" for 127.0.0.1 at 2013-09-10 04:02:56 +0200 Processing by VerificationsController#new as HTML Parameters: {"project_id"=>"4"} Rendered verifications/_form.html.erb (91.3ms) Rendered verifications/new.html.erb within layouts/application (97.5ms)
Rendered layouts/_shim.html.erb (0.3ms) Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."remember_token" = 'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 Rendered layouts/_header.html.erb (38.6ms) Rendered layouts/_footer.html.erb (0.3ms) Completed 200 OK in 381ms (Views: 368.4ms | ActiveRecord: 2.7ms)

and then Create

Started POST "/verifications" for 127.0.0.1 at 2013-09-10 04:03:04 +0200 Processing by VerificationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"sqUBXqA6y5oKCW1DFAi3sv8rQzm+tKjOYDdc/lvUS+c=", "verification"=>{"verifier_name"=>"test", "checked_on(1i)"=>"2013", "checked_on(2i)"=>"9", "checked_on(3i)"=>"10", "notes"=>"test"}, "commit"=>"Create Verification"} Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."remember_token" = 'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 (0.0ms) begin transaction
(0.1ms) rollback transaction Completed 422 Unprocessable Entity in 18ms

ActiveRecord::RecordInvalid (Validation failed: Project does not exist, Project does not exist):
app/controllers/verifications_controller.rb:55:in `create'

EDIT 2

Here's the form for the Create action:

<%= form_for(@verification) do |f| %>
  <% if @verification.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@verification.errors.count, "error") %> prohibited this verification from being saved:</h2>

      <ul>
      <% @verification.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<div class="row">
  <div class="span6 offset3">
  <div class="field">
    <%= f.label :verifier_name %><br />
    <%= f.text_field :verifier_name %>
  </div>
  <div class="field">
    <%= f.label :checked_on %><br />
    <%= f.date_select :checked_on %>
  </div>

  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div></div></div>

<% end %>
2
Look in the Rails log to see what the parameters look like when the user clicks your link. If you post the parameters from the log, the problem will probably be obvious.nachbar
Ok, added to the postkrstck
well you would need to include the project_id in the form you render for the validation. It is getting to the verfication new, but not getting passed into create. You can make it as a hidden field, or another option is to just use a nested resource, see my answer to this question : stackoverflow.com/questions/18701838/… and just substitute project for post and verification for comment.Doon

2 Answers

1
votes

Your new controller action is a bit off. You're assigning an actual id to the @project instance variable, rather that the Project object that corresponds to that id. Subsequently, you're passing an invalid id from your new view to your create action.

Fix things by properly assigning the @project instance variable in your new action:

def new
    @verification = Verification.new(params[:verification])
    @project = Project.find(params[:project_id])
end

Then, you'll need to do a proper lookup of the a Project object in your create action:

def create
    @verification = Verification.new(params[:verification])
    @verification.project = Project.find(params[:project_id])
    @verification.save
end

Finally, add the project_id as parameter in your form via the hidden_field form helper tag:

# in your `create` form
<%= f.hidden_field :project_id, :value => @project.id %>
0
votes

You need to be sure that your project id is passed back to your create method. You can look up the project as in zeantsol's answer, or you can just pass the project ID. But you need to make sure that the form you create in your "new" view passes the project id back to your create method (perhaps in a hidden field)

See this post to see an example of using a hidden field in your view: Rails hidden field undefined method 'merge' error