Not able to delete the data stored in the database pls help?
this is my sages_controller.rb
def delete
#sage = Sage.find(params[:id])
end
def destroy
Sage.find(params[:id]).destroy
redirect_to '/thanksss'
end
this is my delete.html.erb
<%= form_for #sage do |f| %>
<%= f.label :Name %>
<%= f.text_field :Name %>
<br/>
<%= f.label :Email %>
<%= f.text_field :Email %>
<br/>
<%= f.label :Address %>
<%= f.text_field :Address %>
<br/>
<%= f.label :Number %>
<%= f.text_field :Number %>
<br/>
<%= f.submit 'Delete' %>
<% end %>
this is my routes.rb
get 'sages/delete/:id'=> 'sages#delete'
delete 'sages/delete/:id'=> 'sages#destroy'
You don't have to create form for this. button_to should work
<%= button_to "delete", #sage, :method => :delete, :class => :destroy %>
this will internally generate a form with DELETE method, opposed to default GET method like below,
<%= form_for #sage, :method => :delete do %>
You can do something like this,
Change your button to link,
<%= link_to "delete", #sage, :method=>:delete %>
And change your routes file to,
resources :sages,only: [:new,:create,:edit, :update, :delete]
get 'sages/delete/:id'=> 'sages#delete'
Change your delete method like,
def delete
#sage = Sage.find(params[:id])
destroy
end
Related
I am trying out for basic login and registration in ruby on rails,
ISSUE: When I click register button from new.html I am getting GET request but I can see method="post" in page source of that html
I have posted my code below
new.html.erb
<form>
<%= form_for(#user) do |f| %>
<%= f.label :user_name %>
<%= f.text_field :user_name %></br>
<%= f.label :email_id %>
<%= f.text_field :email_id %></br>
<%= f.label :password %>
<%= f.password_field :password %></br>
<%= f.label :college %>
<%= f.text_field :college %></br>
<%= f.label :major %>
<%= f.text_field :major %></br>
<%= f.label :current_job %>
<%= f.text_field :current_job %></br>
<%= f.submit("Create Account",class: "btn btn-primary") %>
<% end %>
</form>
My index.html.erb code which is loaded while application starts
<div class="container">
<div class="row">
<div class="login">
<%= form_tag("/user/login",:method => :post) do %>
<%= label_tag(:EmailId) %>
<%= text_field_tag(:email, params[:email]) %></br>
<%= label_tag(:password) %>
<%= password_field_tag(:password, params[:password]) %></br>
<%= submit_tag("Log In",class: "btn btn-primary") %>
<%= submit_tag("Register",class: "btn btn-primary") %>
<% end %>
</div>
</div>
</div>
My controller code
class UsersController < ApplicationController
def index
#user = User.all
end
def login
print "In Sign in controller"
#user = User.new
if params[:commit] == 'Register'
print "inside Register class"
redirect_to '/users/new'
else
#user = User.find(params[:email_id])
if #user and user.authenticate(params[:password])
sessions[:userId] = #user.user_id
end
end
end
def new
puts "****Inside New Method******"
#user = User.new
end
def create
puts "****Inside create Method******"
end
private
def user_params
end
end
My Route code
Rails.application.routes.draw do
root 'users#index'
resources :users
post '/users/login' => 'users#login'
As per my understanding post request should hit create method, but get /users method is hitting. Please help me out regarding this
Why do you have nested forms in new.html.erb? Remove the first form tag
<form>
<%= form_for(#user) do |f| %>
You have a form tag inside another form tag. Remove the tag at the top of your form. <%= form_for(#user) %> takes care everything that's needed to build the correct form.
I have edit form for company as bellow in edit.html.erb
<%= form_for([:dashboard , #company] ) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>
and my companies_controller.rb
def edit
#company = Company.find(params[:id])
end
def update
# Update code
end
and my routes.rb
namespace :dashboard do
resources :companies , only: [ :edit , :update ]
end
The problem is when submit the form get the bellow error
No route matches [GET] "/dashboard/companies/3"
form_for accepts a post method by default.Here the edit action is get method,so your form_for should look like this
<%= form_for([:dashboard , #company],:html => {:method => :get }) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>
OR
You can do like this too
<%= form_for([:dashboard , #company] :url =>edit_dashboard_company_path(#company)) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>
How would I go about converting this form tag below into a form_for?
<%= form_tag(contact_email_path, :method => 'post') do %>
<%= label_tag "Your email" %>
<%= text_field_tag "sender", #sender, :autofocus => true %>
<%= label_tag "Subject" %>
<%= text_field_tag "subject", #subject %>
<%= label_tag "Message" %>
<%= text_area_tag "message", #message %>
<%= submit_tag "Send Email" %>
<% end %>
form_for is a helper for creating forms which create or edit a resource.
If you have a resource here that you would like to create in your database, you would use this method. What it looks like you're doing here is not creating a resource, but sending an email. If that is the case, then a form_tag is probably a better option.
If you are, however, trying to create a new resource in the database (i.e. an new instance of ContactEmail or some other class), then you could do it like this:
<%= form_for #contact_email do |f| %>
<%= f.label :sender, "Your email" %>
<%= f.text_field :sender, :autofocus => true %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :message %>
<%= f.text_area :message %>
<%= f.submit "Send Email" %>
<% end %>
This assumes that #contact_email is an object that has the methods sender, subject and message and that you have resources :contact_email in your routes file.
I'm new to rails and trying to make a simple site to start learning. When I submit my form, however, the data isn't saved to the db. I'm really not sure what's wrong, I've been trying to figure it out for a while. If I make a record in the rails console and save it, that one successfully shows up in the db (and on the index page).
calculate.rb:
class Calculate < ActiveRecord::Base
attr_accessible :number, :root
end
calculates_controller.rb:
class CalculatesController < ApplicationController
def index
#calculate = Calculate.all
end
def new
#calculate = Calculate.new
end
def create
#calculate = Calculate.new(params[:calculate])
if #calculate.save
redirect_to '/calculates'
else
render 'new'
flash[:notice] = "Didn't work"
end
end
end
new.html.erb:
<%= form_for(#calculate) do %>
<%= label_tag(:number, "Enter the number") %>
<%= text_field_tag :number %>
<%= label_tag(:root, "root") %>
<%= text_field_tag :root %>
<%= submit_tag("Submit") %>
<% end %>
if you are using form_for, use the form_for syntax
<%= form_for(#calculate) do |form| %>
<%= form.label :number %>
<%= form.text_field :number %>
<%= form.label :root %>
<%= form.text_field :root %>
<%= form.submit "Submit" %>
<% end %>
this will automatically handle the routes if the #calculate is new object it will submit it to create or if it is already saved it will send a put request to edit action
Ah hah! I updated my view to:
<%= form_for #calculate, :url => { :action => "create" } do |f| %>
<%= f.label :number %>
<%= f.text_field :number %>
<%= f.label :root %>
<%= f.text_field :root %>
<%= submit_tag("Submit") %>
<% end %>
And now it works. Awesome.
Having problems saving the user_id to the database table Documents. Right now, it's not sending anything associated with user_id for documents when I check the rails server log in POST.
projects_controller.rb
def new_step_3
project = Project.new
#project.documents.build
end
new_step_3.html.erb
<%= form_for #project, :html => {:multipart => true} do |f| %>
<%= f.text_field :title %>
<%= f.text_field :description %>
<%= f.fields_for :documents do |builder| %>
<%= builder.file_field :title %>
<% end %>
<%= f.text_field :skills %>
<%= f.submit 'Post Project' %>
<% end %>
Did u include line accepts_nested_attributes_for :documents in your project model?