Sending form input to controller function - ruby

I'm trying to let user enter an id, then see the children of that ID (via a self-join association). My children function works in the rails console, but not in the controller, because I suspect I'm sending the form input to the controller wrong.
Controller:
def index
..............
if params[:children]
#guideline = Guideline.find(params[:seeChildren])
#guideline.children
else
#guidelines = Guideline.all
end
end
Form:
<%= form_tag(guidelines_path, :method => "get", id: "search-form") do %>
<td><%= label_tag(:seeChildren, "See Immediate Children")%></td>
<td><%= text_field_tag :seeChildren, params[:seeChildren], placeholder: "Enter ID" %></td>
<td><%= submit_tag "Search", :name => nil %></td>
<% end %>
routes:
Prefix Verb URI Pattern Controller#Action
guidelines GET /guidelines(.:format) guidelines#index
POST /guidelines(.:format) guidelines#create
new_guideline GET /guidelines/new(.:format) guidelines#new
edit_guideline GET /guidelines/:id/edit(.:format) guidelines#edit
guideline GET /guidelines/:id(.:format) guidelines#show
PATCH /guidelines/:id(.:format) guidelines#update
PUT /guidelines/:id(.:format) guidelines#update
DELETE /guidelines/:id(.:format) guidelines#destroy
root GET / welcome#index
I appreciate the help! I'm sure it's a stupidly simple solution, but I can't seem to find the answer myself

I suspect I'm sending the form input to the controller wrong.
text_field_tag :seeChildren, params[:seeChildren], placeholder: "Enter ID" %>
Do this instead:
text_field_tag :seeChildren, nil, placeholder: "Enter ID" %>
which will create this html:
<input id="seeChildren" type="text" placeholder="Enter ID" name="seeChildren">
When the user submits the form, one of the name/value pairs sent to the server will be seeChildren="some text", where seeChildren is the name attribute of the textfield, and "some text" is whatever the user entered.
params[:seeChildren] won't exist until after the user submits the form. When the browser sends the request created by the form to the server, rails extracts all the name/value pairs into the params Hash.

Related

Saving data in ruby on Rails without resources

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 !!!
I will help you in solving your problem.
Follow these steps:
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
Now change your view file according to the new route helper:
form_for (#products, url:{:controller=>'products', :action=>'create_product'}, html:{method:'post'})
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

redirect to another URL using submit button in ruby

I have submit button and i want to redirect in another URL (hard coded) this URL
https://www.ccavenue.com/shopzone/cc_details.jsp
my code :
<%= form_tag #ccavanue do |f| , url => "https://www.ccavenue.com/shopzone/cc_details.jsp", :html => :method => "put" %>
<%= hidden_field_tag :my_field, #MerchantId, :id => 'merchant_id' %>
<%= submit_tag "Click Me" %>
<% end %>
i want to redirect another website URL with this submit button . please guided me.
Change your code to following:
<%= form_for #ccavanue, url: "https://www.ccavenue.com/shopzone/cc_details.jsp" do |f| %>
<%= f.hidden_field :my_field, #MerchantId, :id => 'merchant_id' %>
<%= f.submit "Click Me" %>
<% end %>
In Rails a form is designed to create or update a resource and reflects the identity of the resource in several ways:
The url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource),
Input fields should be named in such a way that in the controller their values appear in the appropriate places within the params hash, and
For an existing record, when the form is initially displayed, input fields corresponding to attributes of the resource should show the current values of those attributes.
In Rails this is achieved by creating form using form_for where:
If we want to create any object we use POST method within url and PUT method if we are trying to update an existing record.
Rails framework is smart enough to use POST or PUT method by itself looking at the url of the form. So in this case we need not use method parameter within form_for url.
Probably you can start with Michael Hartl's tutorial

Rails 4 - Controller Edit Action Returning Wrong Record

I have a Comment model which belongs to both User and Story. Creating a comment correctly associated to the appropriate User and Story is working fine but when trying to edit the comment my edit action appears to retrieving the wrong record.
The offending action in comments_controller.rb:
def edit
#story = Story.find_by(params[:story_id])
#comment = #story.comments.find_by(params[:id])
end
The link used to render the comments/edit view:
<%= link_to 'edit', edit_story_comment_path(comment.story_id, comment.id) %>
The corresponding view:
<%= form_for(#comment, url: { controller: 'comments', action: 'update' }) do |f| %>
<%= f.text_area :content %>
<%= f.submit "update" %>
<% end %>
The edit view appears to be rendering the most recently added comment regardless of which #comment I am trying to edit.
You're using find_by, which is basically a magic find_by_X method, with no fields specified. find_by(1) generates invalid SQL for me using Postgres, but it might be that whatever database back-end your using accepts it.
Regardless, find_by certainly won't do what you want it to do.
You should be using find, if you want to find records by id:
#story = Story.find(params[:story_id])

removing a document by passing its id using form_tag

Let's say I have book model, book.rb
class Book
include Mongoid::Document
field :book_id, type: String
field :title, type: String
end
(Here I'm using mongoid, but I think for this question it doesn't matter what type of data is.)
The book model has its own controller, views, etc.
Now, I want to create a page with form_tag (let me know if this is not a proper way), where by entering book's id and clicking enter I'll be able to remove the record this this given id from the database.
remove.html.erb:
<%= form_tag books_path, :method => 'get' do %>
<p>book_id:
<%= text_field_tag :book_id, params[:book_id] %>
<%= submit_tag "Remove", :name => nil, :confirm => "Are you sure?" %>
</p>
<% end %>
I know how to remove a given document, but can't figure out how to pass the value entered in the form and where to put the logic that will remove document.
First things first. Why do you need to store a book_id for your Book model ? Mongoid already provide a _id field for this purpose.
The usual way to destroy resources is to hit the destroy action in your controller by making a DELETE HTTP request.
class BooksController
def destroy
Book.find(params[:id]).destroy
redirect_to :back
end
end
Then simply do a link with the following:
link_to "Delete", book_path(#book), method: :delete
Where #book is your book instance.

Converting from object_id to something useful (name, email, etc) in Rails

I'm on my first Ruby on Rails project and I'm trying to display a list of users in a select box. I want to show all of the users (except the currently logged-in one).
I have that part down right now, using this code in my model, view, and controller:
Request Controller:
def new
#request = Request.new
#users = User.without_user(current_user)
end
New Requests View:
<div class="field">
<%= f.label :user_id, 'Select user' %>
<br />
<%= select_tag(:user_id, options_for_select(#users)) %>
</div>
User Model:
scope :without_user,
lambda{|user| user ? {:conditions =>[":id != ?", user.id]} : {} }
This all works well, but my select box is populated with the object_id of the user. How can I convert that object_id into a first name/last name combination, for example? I tried doing something like:
<%= select_tag(:user_id, options_for_select(#users.first_name)) %>
but that gave me an 'undefined method error.' What would be the best way to handle this?
In the select_tag of your view, you can have:
<%= select_tag(:user_id, options_from_collection_for_select(#users, :id, :first_name)) %>
This would display the first_name and when the user selects one of the options, the user id is what is filled into the value attribute for the select tag.
If you would like to display the full name, you can have a method in your user model:
def full_name
return first_name + " " + last_name
end
And, in your view:
<%= select_tag(:user_id, options_from_collection_for_select(#users, :id, :full_name)) %>
You can find more information about options_from_collection_for_select here
What you need is options_from_collection_for_select.
In your case it would be:
<%= select_tag(:user_id, options_from_collection_for_select(#users, :id, :first_name)) %>
You can read more about it and other helpers here

Resources