Passing variable to the url in rails -


i passing variable that:

new_sub_request_path(request_id:@request.id) 

so url:

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

in controller want assign request_id that:

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

and strong parameters defined:

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

but after save have empty request_id attribute, seems not assigned. doing wrong?

edit: inspecting parameters in console showed have attributes in form.

edit2:

 def create      @sub_request = subrequest.new(sub_request_params)      @sub_request.request_id = params[:sub_request][:request_id]      respond_to |format|        if @sub_request.save          format.html { redirect_to @sub_request, notice: 'sub request 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

you have define below request_id save in sub_requests table.

in create method add line

@request = request.find(params[:request_id]) ,

@sub_request.request_id = @request.id

or

you can add hidden_field in form below

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

and make sure permitting :request_id in sub_request_params


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -