How to post html form data to controller using ruby on rails 3.1 - ruby-on-rails

I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong.
My routes are like:
Rails.application.routes.draw do
root 'first#hello'
get 'first/save'
end
And my html form is like:
<form accept-charset="UTF-8" method='post' action='/first/save'>
<label for='S.No'>S.No</label>
<input type="text" name="s_no" placeholder='Enter s. no.'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='seller_id'>Seller ID</label>
<input type="text" name='seller_id' placeholder='Enter your seller ID'> <br>
<label for='email'>Email</label>
<input type="email" name='email' placeholder='Enter your email'> <br>
<label for='img_url'>Image</label>
<input type='text' name='img_url' placeholder='Enter your image url'> <br>
<input type="submit" name='save' value='Save'>
</form>
And here is my controller:
class FirstController < ApplicationController
def save
#name = params[:name]
#email = params[:email]
#seller_id = params[:seller_id]
#img_url = params[:img_url]
#s_no = params[:s_no]
end
end

If you want to do POST requests, instead of
get 'first/save'
you should have
post 'first/save'

Related

rails adding to database

I am attempting to add product information to a rails sqlite database through a form. I can get users to be added but not the products for the store. This is the form.
<form action='/products/create' method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<label>Name:
<input type="text" name="product">
</label>
<label>Amount:
<input type="number" name="amount">
</label>
<center><input type="submit" value="Sell"></center>
</form>
It does not throw an error but it does not save to db either.
Products_controller is
def create
#products = Product.new(name: params[:name], amount: params[:amount])
if #products.valid?
#products.save
flash[:message] = "New product added"
redirect_to "/users"
else
flash[:errors] = #products.errors.full_messages
redirect_to "/products"
end
I figured it out. The problem was this line.
<input type="text" name="name">
In my database product is defined as name and amount. Product does not have a product field.

"Undefined method 'delete' for nil:NilClass" in Sinatra

I'm working on a project in Sinatra and I can't seem to get the delete method to work. My intent is to be able to remove an object using a form in a modal. Here's what I have:
routes.rb:
delete '/songs/:id/delete' do
#song = Song.where(:id => params[:id]).first
#song.delete
redirect to '/songs'
end
index.erb:
<form action="/songs/:id/delete" method="post">
<input type="hidden" name="_method" value="delete">
<div id="song_id">
<label>id:</label>
<input type="text" name="id">
</div>
<button type="submit" id="delete">Delete</button>
<div id="back">Back to Songs</div>
</form>
Feedback is appreciated.
(Also, sorry indentation isn't perfect)
You have to inject the id into the form. Not :id.
# example
<form action="/songs/1234/delete" method="post">
Also, you can see what is happening with puts params.

I used pry to see what happens when I hit the login button and the information isn't saving to the database

I just learned about pry for debugging in rails, I'm working on a ecommerce site , on the main sign in page, when the login button is clicked, the email, username and the password field of the login form is suppose to save to the database. Instead of getting Example A, I get Example B but I'm not sure what {"controller"=> "application", "action" => "login"} means. Can someone help me please.
Example:A
correct result
Example:B
[wrong result pasted below][1]
23: def login
24: #username = params[:username]
25: #email = params[:email]
26: #password = params[:password]
=> 27: binding.pry
28: if #email && #password
29: session[:signed_in] = true
30: session[:username] = params[:username]
31: redirect_to '/profile'
32: end
33: end
[1] pry(#<ApplicationController>)> #username
=> nil
[2] pry(#<ApplicationController>)> params
=> {"controller"=>"application", "action"=>"login"}
[3] pry(#<ApplicationController>)>
My sign in form:
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>
My routes.rb:
Rails.application.routes.draw do
get '/profile', to:'application#profile'
get '/logout', to: 'application#logout'
post '/login', to: 'application#login'
end
In answer to your specific question
I'm not sure what {"controller"=> "application", "action" => "login"} means
This is a Ruby Hash with two key value pairs that is referenced by your link within the form. The value of the key controller refers to the specific controller that you are linking to with your form. In this case your ApplicationController as defined by the value application. The value of the key action refers to the specific method within that controller that you are calling.
In your form you have <input type="email">, but no name attributes. Name attributes are used by the server to identify the fields in the form.
link check this for more information.
Your params attributes are just {"controller"=>"application", "action"=>"login"} because thats where the form is being submitted, To your application controller and login action, your login action is defined in your form <form action='/login' class='validate-form' method='post'> here /login, thats your action.
In this case login. To get example A, your form should be like this
<input type="email" name='email'>
<input type="text" name='username'>
<input type="password" name='password'>
If you post your form I can try to provide an answer to your other problems.
Change the form to this and try again,
Try adding names to the input fields, and then use the pry and insect the params from it.
Also did you use the strong parameters for the fields?
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email" name="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email" name="username">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password" name="username">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>

Ruby on Rails param is missing or the value is empty without using a model

In my contact.html, I have this code
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="name" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="email" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="subject" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="content" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>
This is under my controller
def contact
end
def send_mail
MessageMailer.new_message(contact_params).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
private
def contact_params
params.require(:contact).permit(:name, :email, :subject, :content)
end
under mailers/message_mailer.rb
class MessageMailer < ActionMailer::Base
default from: "sys.questdentalusa#gmail.com"
default to: "questdentalusa#gmail.com"
def new_message(contact)
#contact = contact
mail subject: #contact.subject
end
end
and under my new_message.text.erb is this code
Name: <%= #contact.name %>
Email: <%= #contact.email %>
Message: <%= #contact.content %>
I am to send an email consisting user's name, email and message which is inputed and NOT saved in the database. When I pass the four parameters like this
def send_mail
MessageMailer.new_message(:name, :email, :subject, :content).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
it worked just fine but i was told to use only one parameter, seems like group the four parameters: name, email, subject, content as one (contact)
When I typed the info and hit the submit button, I get this error message
param is missing or the value is empty: contact
I presume that what caused this error is because my def contact is empty. So I added contact.new and #contact=contact.new and MessageMailer.new but this error occurs NoMethodError
How can I possibly fix this? What should I write under my def contact ?
Your controller code is correct. You don't need Contact.new or something. The problem is with your <form>. What ends up in params depends on your form and in your form you don't have contact.
Instead of:
<input type="text" name="subject">
You have to do something like this:
<input type="text" name="contact[subject]">
And that for all the fields on your contact form.
Another option would be to use Rails' form helpers.
contact[] is missing in your form, see below correct one:
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="contact[name]" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="contact[email]" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="contact[subject]" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="contact[content]" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>

Create a search page in ActiveAdmin

I'm using ActiveAdmin to deploy my project. And I had some problem when I was developing.
I had some database table, e.g: "Worker", "Product", "Task". I wanted to create a page to search on those table with many situation.
I created a simple page:
ActiveAdmin.register_page "my page" do
content do
panel "Search details" do
panel "By Name" do
render :partial => "form"
end
end
end
end
And this is _form.html.erb
<form action="" method="post">
<input type="text" value="" name="taskid">Task ID</input>
<input type="text" value="" name="productid">Product ID</input>
<input type="text" value="" name="workerid">Worker ID</input>
<input type="submit" value="Submit"/>
</form>
But I don't know how can I call a controller from a form? (which controller was defined)
And How can I render or show the result to the content area of "my_page" in Activeadmin from a controller?
Anyone may help me? plz!
Design the form such that it uses the existing active admin filters and displays the results accordingly. You may want to look at the HTML of your filter forms (using firebug), in order to get the required params, and the submit action. Here is the partial which I use to search my User model (constructed from user filters):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search User Email</label><br/>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>
and displays the result in the existing format. You don't need to design new views for that.

Resources