Problem while creating a Record (CRUD) - ruby-on-rails

I tried to save data in my Database using Rail 3 but i could not do that.an
Error occured like this:
NoMethodError in Vorlesungs#new
in my Controller:
class VorlesungsController < ApplicationController
def new
#vorlesung=Vorlesung.new
end
def create
#vorlesung=Vorlesung.create(params[:vorlesung])
if #vorlesung.save
#status_message = 'Student inserted successfully.'
else
render 'new'
end
end
end
and my View:
<%= form_for #vorlesung do |v| %>
Name : <%= v.text_field :Name %> <br>
Name de Professur : <%= v.text_field :Leiter_name %><br>
<%= v.submit 'Speicher'%>
<% end %>
when i changed Form_for into
<%= form_for #vorlesung do |v| %>
an error like this occured:
NoMethodError in Vorlesungs#new
when Form_for remains as:
<%= form_for :vorlesung do |v| %>
after clicking my submit button only the content of textboxes deleted
and no other effect.Thank you very much for your help
that is my full error message:
NoMethodError in Vorlesungs#new
Showing /home/babak/Management/app/views/vorlesungs/new.erb where line #1 raised:
undefined method `vorlesungs_path' for #<#<Class:0xb5f55cc0>:0xb5f54f00>
Extracted source (around line #1):
1: <%= form_for #vorlesung do |v| %>
2: Name : <%= v.text_field :Name %> <br>
3: Name de Professur : <%= v.text_field :Leiter_name %><br>
4: <%= v.submit 'Speicher'%>
and it is my route file:
Management::Application.routes.draw do
# get "vorlesungs/Show"
root :to => 'vorlesungs#Show'
match 'vorlesungs/new' =>'vorlesungs#new'
end

You haven't defined a create action in your routes.
Instead of
# get "vorlesungs/Show"
root :to => 'vorlesungs#Show'
match 'vorlesungs/new' =>'vorlesungs#new'
add
resources :vorlesungs
A good guide on this is to be found here.

Related

AbstractController::ActionNotFound with form_tag in Rails

When I use form_tag as below then I get this error:
AbstractController::ActionNotFound at /quizzes/[whichever_quiz_id]
The action 'whichever_quiz_id' could not be found for QuizzesController
My code is as below:
erb file
<%= form_tag action: :add_questions do %>
<%= collection_select(:quiz, :id, Quiz.where.not(id: params[:id]), :name, :id, prompt: 'Select Quiz') %>
<%= text_field_tag(:input_quiz_questions, 'Question ids') %>
<%= submit_tag "Add" %>
<% end %>
So here I have a method in my QuizController called add_questions which takes a selected question from another Quiz and adds it to the present Quiz.
def add_questions
id = params[:id]
required_quiz_id = params[:quiz][:id].to_i #taken from collection_select
required_questions_ids = params[:input_quiz_questions].split().map { |s| s.to_i } #taken from text_field_tag
# remaining logic here
routes
resources :quizzes, as: 'tests' do
member do
get :add_questions
end
end
So why am I getting the above error here and how can I rectify it?
You should write after form_tag the url, not the name of the action in the option hash
<%= form_tag quizzes_add_questions_path, method: :get do %>

Rails simple form routing issue

I have a form in Rails
<div class="page-header">
<h3>Create Blah</h3>
</div>
<%= simple_form_for #blah do |f| %>
<%= f.input :id %>
<%= f.input :name %>
<%= f.input :pho %>
<%= f.input :fun %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>
When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error
Routing Error
uninitialized constant BlahsController
Here is the BlahController#create method:
def create
authorize! :create, :blahs
#blah = Blah.new(params[:blah])
if #blah.save
redirect_to admin_blah_path(#blah), :notice => 'New blah created!'
else
render :new
end
end
In my rake routes, I have
admin_blahs GET /admin/blahs(.:format) admin/blahs#index
POST /admin/blahs(.:format) admin/blahs#create
new_admin_blah GET /admin/blahs/new(.:format) admin/blahs#new
edit_admin_blah GET /admin/blahs/:id/edit(.:format) admin/blahs#edit
admin_blah GET /admin/blahs/:id(.:format) admin/blahs#show
PUT /admin/blahs/:id(.:format) admin/blahs#update
DELETE /admin/blahs/:id(.:format) admin/blahs#destroy
It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:
<%= simple_form_for [:admin, #blah] do |f| %>
See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.

NoMethodError in Shop#new

I am getting an error when I try to add a barber to my database.
I get the following error:
NoMethodError in Shop#new
Showing /Users/Augus/Rails/Barbershop/app/views/shop/new.html.erb where line #3 raised:
undefined method `barbers_path' for #<#<Class:0x105e7b818>:0x105bfe360>
Extracted source (around line #3):
1: <H1>New barber</H1>
2:
3: <%= form_for #barber do |f| %>
4: <%= f.text_field :name %> <br />
5: <%= f.submit %>
6: <% end %>
I don't know what I am doing wrong.
My shop_controller.rb:
def new
#barber = Barber.new
end
My view new.html.erb:
<H1>New barber</H1>
<%= form_for #barber do |f| %>
<%= f.text_field :name %> <br />
<%= f.submit %>
<% end %>
<%= link_to 'Back', shop_path %>
I also get this in my routes:
resources :shop
By default Rails tries to figure out the path for sending forms based on the object you pass in. In your case Rails tries to build path for sending the form based on your #barber object. As your routes.rb shows, you haven't written resources :barbers and your form fails to find proper path. You have to specify your path directly. Like this way:
<%= form_for #barber, url: shop_path do |f| %>
When you submit a new record form, the function controller#create is called. controller#new is the function that generates the page containing the form.
You need a BarberController with #create method.
Create the resource like this:
resources :shop do
resources :barber
end
A shop contains barbers.
Then form_for [#shop, #barber], :action => 'create' will trigger BarberController#create the last line of which should probably redirect to shop.
Instead of:
<%= form_for #barber do |f| %>
Try this:
<%= form_for(#barber) do |f| %>

Why is this rails form not finding the correct path?

So I've been banging my head against the wall trying to figure out why this isn't working. I keep getting
ActionView::Template::Error:
undefined method `admin_information_index_path' for #<#<Class:0x007fc67971cab8>:0x007fc67d775740>
With the trace:
# ./app/views/admin/informations/_form.html.erb:1:in `_app_views_admin_informations__form_html_erb__2815737811953353352_70245242566200'
# ./app/views/admin/informations/new.html.erb:2:in `_app_views_admin_informations_new_html_erb___3700624853666844924_70245242606040'
Any tips in the right direction?
My routes:
namespace :admin do
resources :informations
end
My controller:
class Admin::InformationsController < Admin::AdminController
def new
#information = Information.new
end
end
views/admin/informations/new.html.erb:
<h1>Add New Information Page</h1>
<%= render :partial => 'form', locals: { information: #information } %>
views/admin/informations/_form.html.erb:
<%= form_for [:admin, information] do |f| %>
<%= error_messages_for information %>
<%= f.label :title %><br>
<%= f.text_field :title %><br><br>
<%= f.label :content %><br>
<%= f.text_area :content %><br><br>
<%= f.submit "Submit" %>
<% end %>
Output of rake routes
admin_informations GET /admin/informations(.:format) admin/informations#index
POST /admin/informations(.:format) admin/informations#create
new_admin_information GET /admin/informations/new(.:format) admin/informations#new
edit_admin_information GET /admin/informations/:id/edit(.:format) admin/informations#edit
admin_information GET /admin/informations/:id(.:format) admin/informations#show
PUT /admin/informations/:id(.:format) admin/informations#update
DELETE /admin/informations/:id(.:format) admin/informations#destroy
admin_root /admin(.:format) admin/sessions#new
Try just
<%= form_for information ,:namespace=>'admin' do |f| %>
UPDATE:
Look at your routes 'informations' pluralized, but your using the singular form 'information'
You must use correct form of controller, because:
'information'.pluralize
is
"information", not informations.
So, rename controller and view folder.
I'm not sure if this will work... Just a guess.
form_for #admin.information or something along those lines.

customized form_for tag in rails

I want to make a table within a form by making a new form_tag. The following code in ApplicationHelper fails:
module ApplicationHelper
class TabularFormBuilder < ActionView::Helpers::FormBuilder
# ... code to insert <tr> tags </tr>
end
def tabular_form_for(name, object = nil, options = nil, &proc)
concat("<table>", proc.binding)
form_for(name,
object,
(options||{}).merge(:builder => TabularFormBuilder),
&proc)
concat("</table>", proc.binding)
end
end
The view I use is:
<h1>New project</h1>
<% tabular_form_for :project, :builder => ApplicationHelper::TabularFormBuilder do |f| %>
<%= f.error_messages %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<%= f.text_field :location %>
<%= f.submit 'Create' %>
<% end %>
The error I get is:
NoMethodError in Projects#new
Showing app/views/projects/new.html.erb where line #5 raised:
undefined method `errors' for {:builder=>ApplicationHelper::TabularFormBuilder}:Hash
Any ideas how to make this custom tag work?
Is this posted verbatim? Because your second block needs to be within the closing end tag for it to access the FormBuilder class right?
I found the following tutorial which might help:
http://ramblingsonrails.com/how-to-make-a-custom-form-builder-in-rails

Resources