I’m trying to generate a link that creates a new object. It creates the object, but without setting the user_id or subreddit_id.
<%= link_to ' (add to my subreddits)',
{
controller: 'subscriptions',
action: 'create',
user_id: current_user.id,
subreddit_id: subreddit.id
},
method: :post %>
here are the relevant controllers:
def new
@subscription = Subscription.new
unless current_user.admin?
@subscription.user_id = current_user.id
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @subscription }
end
end
def create
@subscription = Subscription.new(params[:subscription])
unless current_user.admin?
@subscription.user_id = current_user.id
end
respond_to do |format|
if @subscription.save
format.html { redirect_to @subscription, notice: 'Subscription was successfully created.' }
format.json { render json: @subscription, status: :created, location: @subscription }
else
format.html { render action: "new" }
format.json { render json: @subscription.errors, status: :unprocessable_entity }
end
end
end
I guess your error is here:
You don’t set the value for
subscriptionparameter. You setparams[:user_id]andparams[:subreddit_id]. Use them on creating your Subscription, like: