(ruby 1.9.2 rails 3.0.6)
I am new to ROR, sorry for my simple question.
I have a form and a controller, when i launch my app, i can see the log have been displayed.. however there are exception when rendering the view...
Everything should look just fine.. what is the problem? how can I fix it?
Thanks.
undefined method `users_path' for #<#<Class:0x47c7678>:0x47c6118>
Extracted source (around line #2):
1: <h1>Welcome Aboard</h1>
2: <%= form_for(#user) do |f| %>
3: <table>
4: <tr>
5: <td><%= f.label :username %></td>
class RegisterController < ApplicationController
def sign_up
logger.debug("sign_up inoked")
#user = User.new
logger.debug("sign_up finished")
end
end
views/register/sign_up.html.erb
<%= form_for(#user) do |f| %>
<table>
<tr>
<td><%= f.label :username %></td>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<td><%= f.label :password %></td>
<td><%= f.password_field :password %></td>
</tr>
<tr>
<td><%= f.label :password_confirmation %></td>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"><%= f.submit "Sign up" %></td>
</tr>
</table>
<% end %>
It seems that you do not have the appropriate routes. Do you have a line like :
resources :users
in your routes.rb file ? If not try adding that please. Or just create the particular users_path named route.
Related
Rails new.html.erb is not recieving values or not passing values to the controller. from to get new values.
following is my new.html.erb
def new
#departmentdetail=DepartmentDetail.new
end
def create
#departmentdetail=DepartmentDetail.new(department_param)
if #departmentdetail.save
redirect_to ('index')
else
redirect_to('new')
end
end
def department_param
params.require(:departmentdetail).permit(:departmentname, :departmentemail, :password)
end
<%= form_for(#departmentdetail) do |f| %>
<table summary="Subject for fields">
<tr>
<th> <%= f.label :departmentname %><br></th>
<td><%= f.text_field :departmentname %></td>
</tr>
<tr>
<th> <%= f.label :departmentemail %><br></th>
<td><%= f.text_field :departmentemail %></td>
</tr>
<tr>
<th> <%= f.label :password %><br></th>
<td><%= f.text_field :password %></td>
</tr>
</table>
<%= f.submit("Create Department")%>
You are not fully following Ruby on Rails naming conventions. When there is a class named DepartmentDetail then the variables and the keys in the strong params should be named with an underscore like this department_detail. Additionally, redirecting to new instead of just rendering new in the case of an error when saving will result in losing the current state:
I suggest changing your controller to:
def new
#department_detail = DepartmentDetail.new
end
def create
#department_detail = DepartmentDetail.new(department_detail_params)
if #department_detail.save
redirect_to :index
else
render :new
end
end
def department_detail_params
params.require(:department_detail).permit(:departmentname, :departmentemail, :password)
end
And your view to:
<%= form_for(#department_detail) do |f| %>
<table summary="Subject for fields">
<tr>
<th> <%= f.label :departmentname %><br></th>
<td><%= f.text_field :departmentname %></td>
</tr>
<tr>
<th> <%= f.label :departmentemail %><br></th>
<td><%= f.text_field :departmentemail %></td>
</tr>
<tr>
<th> <%= f.label :password %><br></th>
<td><%= f.text_field :password %></td>
</tr>
</table>
<%= f.submit("Create Department")%>
<% end %>
When the user submits an invalid form you want to render - not redirect.
def create
#departmentdetail = DepartmentDetail.new(department_param)
if #departmentdetail.save
redirect_to action: :index
else
render :new
end
end
This will render the new.html.erb view so that the user gets the form back but with messages about the validation errors.
On a technical level what you're actually doing here is rendering a response to the user sending a non-idempotent request - its a response custom tailored to that request. This is very different from the new action which is idempotent (the form looks the same for everyone).
If you redirect the user you'll lose all the user input and the validation messages.
I am new in rails
I have 7 fields in my forms in rails 5 and validations on some of them
When i am filling all correct data it's working fine.
When i do not fill any field which is mandatory it shows error message problem is it clear all the 4 or 5 or 6 textboxes and i need to fill all the same data again.
Is there any option to retain the data and show alert message?
I have tried redirect_to and render both but both are not working and refreshes the page and clear all the data.
def create
if user.save
redirect_to user_path, notice: 'User saved'
else
#redirect_to new_user_path, alert: user.errors.full_messages.first
flash[:alert] = user.errors.full_messages.first
render '/admin/user/new'
end
end
new.html.erb
<%= form_with(url: create_user_path+'?'+request.query_string, id: 'new_user', class: 'new_user', local: true) do |f| %>
<%= render 'form', f: f %>
<br><br>
<%= f.submit 'Submit' %>
<% end %>
_form.html.erb
<table class='mui-table mui-table--bordered'>
<%= f.fields_for :user do |f| %>
<tr>
<td><strong>First Name</strong></td>
<td><%= f.text_field :first_name %></td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td><%= f.text_field :last_name %></td>
</tr>
<tr>
<td><strong>Name of Bank</strong></td>
<td><%= f.text_field :bank_name %></td>
</tr>
<tr>
<td><strong>Branch Name</strong></td>
<td><%= f.text_field :branch_name %></td>
</tr>
<tr>
<td><strong>Account Name</strong></td>
<td><%= f.text_field :account_name %></td>
</tr>
<tr>
<td><strong>Account Number</strong></td>
<td><%= f.text_field :account_number, { onkeypress: 'return isNumberKey(event)' } %></td>
</tr>
<tr>
<td><strong>Membership Number</strong></td>
<td><%= f.text_field :sf_membership_number, { onkeypress: 'return isNumberKey(event)' } %></td>
</tr>
<% end %>
</table>
Create method is above.
Two steps need to follow:-
Use form_for instead of form_with
remove <%= f.fields_for :user do |f| %>
Its works
I have Pages associated to my Categories
Now I have a form that I'm creating Pages
<%= form_for #page do |f| %>
<table>
<tr>
<td><%= f.label :title %></td>
<td><%= f.text_field :title %></td>
</tr>
<tr>
<td><%= f.label :desc %></td>
<td><%= f.text_field :desc %></td>
</tr>
<tr>
<td><%= f.label :category_id %></td>
<td><%= f.select(Page.all, :category_id, :title) %></td>
</tr>
</table>
<%= f.submit %>
<% end %>
I'm trying to create a select dropdown that will show me all the categories that I have from my db, after I select one it will assign the Page that I'm creating to the Category that I'm choosing from the select dropdown
You should use
f.select :category, Category.pluck(:title, :id)
Be aware, that if your rails version is lower, that 4.x you can only use pluck with one column.
You need to change your select tag according to this.
<%= form_for #page do |f| %>
<table>
<tr>
<td><%= f.label :title %></td>
<td><%= f.text_field :title %></td>
</tr>
<tr>
<td><%= f.label :desc %></td>
<td><%= f.text_field :desc %></td>
</tr>
<tr>
<td><%= f.label :category %></td>
<td><%= f.select(:category_id, options_from_collection_for_select(Category.all, :category_id, :title)) %></td>
</tr>
For Rails3(or may be less), if you've a Category model you can simply do.
f.select(:category, Category.select([:id,:title]).map { |c| [ c.id, c.title ] } , { include_blank: true })
collection_select is what you need:
<%= form_for #page do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :title %>
<%= f.submit %>
<% end %>
Hi im new to ruby on rails. And im exploring the codes on it. Ive got this error line of code
undefined method `permit' for nil:NilClass
here's my controller below
class AddsController < ApplicationController
def new
#add = Add.new
end
def create
#add = Add.new(params[:post].permit(:first_name,:last_name,:email))
if #add.save
redirect_to(:controller=>'home')
else
render 'new'
end
end
end
my new.html.erb
<h1>Add Record </h1>
<%= render 'form' %>
<%= link_to "Back", controller: "home" %>
and my _form.html.erb
<%= form_for #add do |f| %>
<table>
<tr>
<td>FirstName: </td>
<td><%= f.text_field :first_name %></td>
</tr>
<tr>
<td>LastName: </td>
<td><%= f.text_field :last_name %></td>
</tr>
<tr>
<td>Email: </td>
<td><%= f.text_field :email %></td>
</tr>
<tr>
<td> </td>
<td><%= f.submit "Add", :class => 'btn btn-success' %></td>
</tr>
</table>
<% end %>
can someone help me figured out the error?
It should look like:
params.require(:add).permit(:first_name, :last_name, :email)
I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>