Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting params value in rails create method from controller

My code has send s

    <% url = url_for(:controller => 'boxelements', :action => 'new', :project_id => @project.id, :author_id => User.current.id)  %>
    <%= link_to "Upload New File",url %>

from new method I can get the param values

    def new
      @boxelement = Boxelement.new
      puts params[:project_id]
      puts params[:author_id]
    end

Here is how I send this value as a form input

    <%= f.hidden_field :project_id, :value =>project_id %>
    <%= f.hidden_field :author_id, :value =>User.current.id %>

I need to access those value from create method of the controller

    def create
      @boxelement = Boxelement.new(params[:boxelement])

      if @boxelement.save
        puts params[:project_id] 
        puts params[:author_id]
      end
    end

This code shows nothing when they are in create method of controller.

    puts params[:project_id]
    puts params[:author_id]

What's wrong with my code?

like image 539
Reza Avatar asked Sep 22 '26 17:09

Reza


1 Answers

From this line I can assume you want to access params of :boxelement

     @boxelement = Boxelement.new(params[:boxelement])

use the following line in order to access the params value.

 puts params[:boxelement][:project_id] 
 puts params[:boxelement][:author_id]
like image 140
Opu Avatar answered Sep 24 '26 07:09

Opu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!