ruby on rails - Attributes sent from link_to to create action is not saved -
i have following link_to in index.html.erb. whenever click on link, create new record in retrieval_requests table reason user_id , package_id not saved.
i'm not sure on how go this. appreciate help.
thanks.
index.html.erb
. . <%= link_to "retrieve package", retrieval_requests_path(user_id: current_user.id, package_id: item.package.id), :method => :post %> . .
retrieval_requests_controller.rb
class retrievalrequestscontroller < applicationcontroller def index @items = item.where(:user_id => current_user.id) end def create @retrieval_request = retrievalrequest.new(params[:retrieval_request]) if @retrieval_request.save redirect_to retrieval_requests_path, notice: "successfully created retrieval request." else render :new end end end
since you're using link , not form, you're going have create record individual params.
so instead of using params[:retrieval_request]
(which don't exist), use params[:user_id]
, params[:package_id]
.
when pass parameters link helper, such as:
retrieval_requests_path(user_id: 3, package_id: 26)
it create url looks following, depending on how set routes:
"/retrieval_requests/3/26" # or no user_id or package_id set in routes: "/retrieval_requests?user_id=3&package_id=26"
then in controller need grab params separately.
Comments
Post a Comment