Reputation: 6657
I am attempting to allow a user to create a campaign given they have selected a certain restaurant, the models are:
class Restaurant < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
has_many :campaigns, dependent: :destroy
end
class Campaign < ActiveRecord::Base
belongs_to :restaurant
end
That said, I have the methods in the Campaigns controller to create a new campaign and then to build (below).
def new
if signed_in?
# create new campaign
@campaign = Restaurant.find(params[:id]).campaigns.new
else
redirect_to signin_path
end
end
def create
@campaign = Restaurant.find(params[:restaurant_id]).campaigns.build(campaign_params)
if @campaign.save
flash[:success] = "Campaign created!"
redirect_to current_user
else
render 'new'
end
end
The user progress in the app essentially is: While on the restaurant show page the user clicks on create a campaign, accessing the new
method in campaign controller.
The view for a new campaign, without the partial, is below and as can be seen it accepts the @campaign
from new
.
<h1>New Campaign</h1>
<div class="row">
<div class="span6 offset3">
<%= nested_form_for @campaign do |f| %>
<%= render 'parameters', f: f %>
<div class="span6 pull-right" style="text-align:right">
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
</div>
As I attempt to submit the form, the error arises with the campaign controllers create
method at @campaign = Restaurant.find(params[:restaurant_id]).campaigns.build(campaign_params)
noting Couldn't find Restaurant without an ID
As displayed, the campaign belongs to a restaurant with the id in campaign models field :restaurant_id
How can I make this work to pass the restaurants id so the campaign can be created?
Upvotes: 0
Views: 126
Reputation: 1582
To add a parameter to a form, use the hidden_field helper just above the submit tag:
<%= f.hidden_field :restaurant_id, @campaign.restaurant.id %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
This should be available in the create method of your controller as:
params["campaign"]["restaurant_id"]
If you for some reason want it to come in simply as params["id"], you would need to use a hidden_field_tag like so:
<%= hidden_field_tag :id, @campaign.restaurant.id %>
Side note - for debugging, you should use the debugging tool called "pry". This will change your life.
Simply add to your Gemfile:
gem 'pry'
then bundle and restart your server:
bundle
rails s
Then put a 'binding.pry' anywhere you want (in a view, model, helper, or controller) - the server will pause at the place where you put the binding.pry and you can poke around in the server console.
I would put the binding.pry right inside your create method:
def create
binding.pry
# rest of code here
end
Then submit the form like normal, and check your server screen in the terminal... you should see the Pry prompt.
Type:
params
to see all of the parameters coming through the form submit. to exit, type exit or ctrl+c
Remember to remove the binding.pry when you are done!!!
Upvotes: 1