Hello I am beginner to ruby on rails and I am making a form for taking student's info. I am facing the problem here:
My view code is:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#userinfo) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
My controller code is:
def new
#userinfo= Userinfo.new
end
My routing is:
get 'about' => 'userinfo#new'
I am getting the error: "undefined method `userinfos_path' for #<#:0x007f05d8fa9be8> "
I am beginner to this. How to solve this problem. Need help. Thank you.
You don't have a route for userinfos.
try adding in your config/routes.rb:
resources :userinfos
I hope this help.
The reason why it gives you exceptions is you're using form_for(#userinfo) simply in your view which intelligently generates form action for you.
Simply, you can specify the form attributes, like
form_for(#userinfo, url: '/about/create'), for more Rails way, you'll need to add method: :put
Here's the example:
ruby
form_for #userinfo, url: '/about/create', method: :put, html: { class: 'form-horizontal' }
Otherwise, #Archie Reyes 's answer works great.
Related
In the controller review_queue I have a custom action that posts a result to a target URL, I want to build a form for this action. I am not going to save any of the fields to the DB I am just going to pass them in the params to the post_review action.
def post_review
RestClient::Request.execute(:method => :post,
:url => Rails.application.secrets['target_url'],
:content_type => :json,
:payload => #result_params.merge!(params[:reasons]).to_json,
:headers => HEADERS)
end
In the view I have a form that will be filled out and on submit it should send up the reasons when the form is submited, I am setting the review_queue_id and the status in the form, since these are static, but the reasons should come from the textarea
<%= form_for(:review_queue, url: { action: 'post_review', :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= f.text_area(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= f.submit 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
error message:
NoMethodError - undefined method `reasons' for #<ReviewQueueApplication:0x007fa7ff7832d8>:
It seems as if rails is assuming the MVC architecture here, and assuming I want to pass the reasons to the review_queue model. there is no reasons column so it's dropping a no method error. Is there a way of specifying that the form is 'temporary' and only getting as far as the controller?
This seems like it should be a simple thing but there is some rails magic happening here.
NoMethodError - undefined method `reasons' for
ReviewQueueApplication:0x007fa7ff7832d8
form_for assumes that you are creating a form for a model object and expects the fields to be present in that specific model's table(in a normal situation).
You should be going with form_tag
<%= form_tag post_review_path, method: :get, :review_queue_id => #review_queue.id, :status => 'accepted'} ) do |f| %>
<div class='form-group'>
<label for='comment'>Please give a reason? (required)</label>
<%= text_area_tag(:reasons, placeholder: 'Your commentns ...', rows: 9, class: 'form-control') %>
</div>
<div class='modal-footer'>
<%= submit_tag 'Approve', class: 'btn btn-success btn-decission btn-modal-left-side' %>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
<% end %>
And in the controller access it like params[:reasons]. Also if you noticed, I've added method: :get to the form_tag as you don't want to save the info to DB
The rails helper form_for is used for forms for rails resources. You want to use the form_tag helper. Search for form_for and form_tag here for more information on these 2 methods.
I'm having an issue very similar to the one asked in this question here: NoMethodError / undefined method `foobar_path' when using form_for However the answer there confuses me.
I went through Michael Hartel's Ruby on Rails tutorial before developing the application I'm working on at the moment, I tried to copy exactly what he did when he created a user model as I created my model. My application is designed to be a database for university professors, so the model I'm using is called "professor" but it's the same concept as "user".
Here is the code for my New.html.erb where is where users go to create a new professor:
<%provide(:title, 'Add a professor') %>
<div class="jumbotron">
<h2> New Professor</h2>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for (#professor) do |f| %>
<%= f.label "First Name" %>
<%= f.text_field :fname %>
<%= f.label "Last Name" %>
<%= f.text_field :lname %>
<%= f.label "School" %>
<%= f.text_field :school %>
<%= f.submit "Add this professor", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
And then here is the code from the Professor_controller.rb
class ProfessorController < ApplicationController
def show
#professor = Professor.find(params[:id])
end
def new
#professor = Professor.new
end
end
When I replace
<%= form_for (#professor) do |f| %>
In new.html.erb with:
<%= form_for (:professor) do |f| %>
It works. The thread I mentioned above said something about adding a route for the controller. My routes.rb looks like this:
Rails.application.routes.draw do
root 'static_pages#home'
get 'about' => 'static_pages#about'
get 'newprof' => 'professor#new'
resources :professor
And I don't believe that in Michael Hartel's book he does anything differently. I'm still very new to Rails so forgive me if this is a bit of an easy question, I've been stuck on it for a few days and I've tried numerous work arounds, using the instance of :professor works but #professor does not and I don't know why.
Within the Rails environment it's very important to be aware of the pluralization requirements of various names. Be sure to declare your resources as plural:
resources :professors
Declaring it in the singular may mess up the automatically generated routes, you'll get thing like professor_path instead of professors_path. You can check what these are with:
rake routes
If you get errors about x_path being missing, check that there's a route with the name x in your routes listing. The most common case is it's mislabeled, a typo, or you've failed to pluralize it properly.
I want to pass one variable which is introduced by the user in the front-end. I dont use any model (i dont need it, because im working with JSON data all in memory). I have looked many tutoriasl but almost all of them are focused on filling out a form. My application does not have any form nor tables. Any idea?
Thank you.
{<div class="module1">
<p>Mein Lastprofil berechnen</p>
<div class="boxed">
Jahreshausverbrauch (kWh)
<%= text_field_tag "input", nil, placeholder: "3500" %>
<%= button_to "Senden", root_path, :method => :get %>
</div>
</div>}
I want to save/pass the variable introduced when clickling the button.
By having a text field you implicitly already have a form.
Make it explicit by wrapping the text field and button:
<%= form_tag root_path, method: :get do %>
<%= text_field_tag :input, placeholder: "3500" %>
<%= submit_tag "Senden" %>
<% end %>
Then you can access the value as params[:input] in the controller.
The -Form_html.erb, the routes.rb and the create method in the controller are as below. But on form submission, it gives nil class error the moment I use params[:mail_setting]
routes.rb
-----------
resources :mail_settings
the _form.html.erb
---------
<%= form_tag '/mail_settings' do %>
<div class="fieldBlock">
<%= label_tag :name %> <%= text_field_tag :name%> </div>
<div class="fieldBlock">
<%= label_tag :id%> <%= text_field_tag :id%> </div>
<div class="actions fieldBlock">
<%= submit_tag "Update Settings ", :class => "btn-large btn-success" %>
</div>
<% end %>
The create method in controller:
def create
#mail_setting = MailSetting.find_by_user_id_and_name(current_user.id, params[:mail_setting][:name])
if ! #mail_setting.blank?
#mail_setting.update_attributes(params[:mail_setting])
else
#mail_setting = MailSetting.new(params[:mail_setting])
#mail_setting.save
render action: "index"
end
end
Try using params[:mailsetting] without the underscore. The model is named MailSetting, so I assume that would be right. The NoMethod error means that there is no generated helper method by the name of mail_setting, which, since you already made the model, is probably because it's spelled wrong.
I would recommend not using underscores/dashes when naming your resources anyways since it just confuses the grammar.
I'm trying to nest a form within another using submit_to_remote but it does a PUT instead of a POST. Can anyone explain what's wrong here?
The routes are RESTful:
map.resources :thing
map.resources :item
The view is like this:
<% form_for(#thing) do |f| %>
<% fields_for(Item.new) do |i| %>
<%= i.text_field :name %>
<%= submit_to_remote 'create', 'Create', :url => items_path, :method => "post" %>
<% end %>
<%= f.text_field :title %>
<%= f.submit 'Update' %>
<% end %>
To get around the problem I've been adding another method to the restful routes to do a create on a PUT but it's ugly and I want to know what the problem is.
The submit_to_remote comes out as:
<input name="create" onclick="new Ajax.Request('/items', {asynchronous:true, evalScripts:true, method:'post', parameters:Form.serialize(this.form) + '&authenticity_token=' + encodeURIComponent('blah')});" type="button" value="Create">
Thanks
How about using link_to_remote instead and style the link to like a 'button', or just leave it as a link would be fine to be honest. This way you can control the XmlRequest fully. Currently I think the method is being determined by your actual form that is being submitted by the JS, not the :method your setting in the helper call.