0

Its bit difficult for me to explain what I want, but still I will try my best.

I created a form in Rails where user can fill certain fields. Now once these fields are filled, I know that I can use resources in Router and define a create method in Controller that will save the data to database. However what I want is to pass data saved in the form to my controller. Then create a custom method in controller that will be just like traditional Create method, but instead of passing parameters using Resource method, I want to pass them as parameter. Is it even possible in Rails:

This is my current View to create form:

<h1> Please add a new product</h1>
<%= form_for @product do |p| %>
  <p>
    <%= p.label 'Product Name' %><br/>
    <%= p.text_field :product_name %><br/>
  </p>
  <p>
    <%= p.label 'Description' %><br/>
    <%= p.text_area :description %><br/>
  </p>
  <p>
    <%= p.label 'Price' %><br/>
    <%= p.text_field :price %><br/>
  </p>
  <p>
    <%= p.label 'Rating' %><br/>
    <%= p.text_field :rating %><br/>
  </p>

<% end %>

So may be I am using In Built form in Ruby, but I just want to pass parameters from View to Controller's method.

Thanks for help !!!

1

1 Answer 1

1

I will help you in solving your problem. Follow these steps:

  1. Create your route in routes.rb:

    get "/create_product" => 'products#create_product', as: :create_product
    

    or if you want to pass params through post method:

    post "/create_product" => 'products#create_product', as: :create_product
    
  2. Now change your view file according to the new route helper:

    form_for (@products, url:{:controller=>'products', :action=>'create_product'}, html:{method:'post'})
    
  3. Now the last step modify your controller:

    def create_product
     #your form values are avaible here in params variable
     pp params
     puts params[:Price]
     #save your params into ur db
    end
    

Note: I assumed that you already have product.rb model

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.