0

I am passing variable like that:

new_sub_request_path(request_id:@request.id)

so i get this url:

http://localhost:3000/sub_requests/new?request_id=1

In my controller i want to assign that request_id like that:

@sub_request = SubRequest.new(sub_request_params)
@sub_request.request_id = params[:request_id]

and my strong parameters are defined:

def sub_request_params
   params.require(:sub_request).permit(:description, :diagnos, :price, :payment, :request_id)
end

But after save i have empty request_id attribute, so it seems that it is not assigned. What am I doing wrong?

EDIT: Inspecting parameters in the console showed that i only have those attributes that are in my form.

EDIT2:

 def create
    @sub_request = SubRequest.new(sub_request_params)
    @sub_request.request_id = params[:sub_request][:request_id]
    respond_to do |format|
      if @sub_request.save
        format.html { redirect_to @sub_request, notice: 'Sub request was successfully created.' }
        format.json { render :show, status: :created, location: @sub_request }
      else
        format.html { render :new }
        format.json { render json: @sub_request.errors, status: :unprocessable_entity }
      end
    end
  end

5
  • 1
    Try @sub_request.request_id = params[:sub_request][:request_id] Commented Jul 14, 2015 at 8:10
  • @Pavan, no it doesn't help Commented Jul 14, 2015 at 8:11
  • Please post that full controller method code. Commented Jul 14, 2015 at 8:14
  • Do you have a model called Request? Commented Jul 14, 2015 at 8:25
  • @Pavan yes, request has_many sub_requests, and sub_request belongs_to request Commented Jul 14, 2015 at 8:50

1 Answer 1

1

You have to define something like below for the request_id to save in sub_requests table.

In your create method add this line

@request = Request.find(params[:request_id]) and do

@sub_request.request_id = @request.id

or

You can just add a hidden_field in your form like below

<%= f.hidden_field :request_id, :value => @request.id %>

And make sure you are permitting :request_id in your sub_request_params

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.