I'm trying to get the Wicked Wizard gem working on my Rails app. Once a user registers for the app (using Devise), they're redirected to this form:
income.html.erb
<%= form_for #finance, url: wizard_path, :method => :put do |f| %>
<div class="field">
What <strong>year</strong> were you born?<br>
<%= f.number_field :age %>
</div>
<div class="field">
What's your <strong>zip code</strong>?<br>
<%= f.number_field :zip %>
</div>
<%= f.submit %>
<% end %>
I generated a controller called finances_welcome_controller.rb that handles wizard_path:
class FinancesWelcomeController < ApplicationController
before_filter :authenticate_user!
include Wicked::Wizard
steps :income, :expenses
def show
#finance = Finance.find_all_by_user_id current_user[:id] || #finance = current_user.finances.build(finance_params)
render_wizard
end
def update
#finance = Finance.find_all_by_user_id current_user[:id]
#finance.update(params[:finance])
render_wizard #finance
end
When I click the submit button, I'm getting this error:
NoMethodError in FinancesWelcomeController#update
undefined method `update' for #<Array:0x00000104c6ff48>
Extracted source (around line #14):
def update
#finance = Finance.find_all_by_user_id current_user[:id]
**#finance.update(params[:finance])**
render_wizard #finance
end
Not sure why the update method hasn't been defined since this is the same syntax that my resource's model is using. The Wicked Wizard gem was successfully implemented on this app.
a method starting find_all_by will return an array of Active record instances... not just a single one. update only works on a single instance.
So, either you want to run through all the instances in the array... using an each - or you want to just fetch the first one using find_by instead of find_all_by
In the case of finding by an id... I'd recommend changing it to find_by
so:
#finance = Finance.find_by_user_id current_user[:id]
#finance.update_attributes(params[:finance])
Related
So, the set up is very simple. In my controller I have
class DriverController < ApplicationController
def index
end
def new
#driver = Driver.new
end
end
And the new.html.erb is
<h1>Driver#new</h1>
<%= form_for #driver do |f| %>
<%= f.submit %>
<% end %>
When I try to acess that new page it says: "undefined method `new' for Driver:Module"
When I change def new in my controller to def create for an example, this error goes away and it says that First argument in form cannot contain nil or be empty. What is the problem?
<h1>Driver#new</h1>
<%= form_for :driver, :url: drivers_path do |f| %>
<%= f.submit %>
<% end %>
In routes.rb
resourse :drivers
And further I would suggested you yo Visit Guide Ruby on Rails the-first-form
Hope this help you !!!
I don't know where the problem was, but I just did everything all over again, and it worked.
I have two controller in my application events and registrations. I want to send event_id of current event to registrations controller after form submission in event show template
show.html.erb
<%= form_for #registrations do |f| %>
<%= f.submit %>
<% end %>
registrations_controller
class RegistrationsController < ApplicationController
def create
#event = Event.find(params[:event_id])
end
end
but it gives me following error
cannot find event with id =
What i think that since event and registrations are not related in any way.So its giving this error.
How to solve this error
Without knowing more about the model associations and also what the #registrations object is, this is the easiest solution:
Add a hidden field to your form:
<%= f.hidden_field( :event_id, #event.id ) %>
# assumes #event exists in the show action somewhere
and in your controller:
def create
#event = Event.find(params[:registrations][:event_id])
end
If your models are associated you can do something like:
<%= form_for #event.registrations.new %>
but it's hard to say if that will work in your app based on the code provided.
I am trying out ruby on rails, going through the "Getting started with Ruby" tutorial. I have gone through steps to and including 5.7 and by now I should be able to create a new post and view it, the post is created, but the title and text is not showing.
The controller: posts_controller.rb
class PostsController < ApplicationController
def new
end
def create
#post = Post.new(params[post_params])
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
The view to create the post
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The show view for showing the post.
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
<strong>Text:</strong>
<%= #post.text %>
</p>
My guess is that the issue is with the passing of data to the database, but i can't figure out whats wrong. Any idea?
In the new controller action, be sure to assign a new instance of Post to an instance variable:
# app/controllers/posts_controller.rb
def new
#post = Post.new
end
Then, pass the #post instance variable to the form_for helper in the view:
# app/views/posts/new.html.erb
<%= form_for #post do |f| %>
The form_for helper expects a resource to be passed to it; the form is automatically submitted to the correct route based on the type of resource passed. You're passing a new resource in this instance, so the form is routed to the resource's create action.
I figured out that this line was the issue
#post = Post.new(params[post_params])
It should be
#post = Post.new(post_params)
Thanks to everybody who answered!
you may want to change the title of this post to something like "data not showing in view". here is what i would make sure of: that you have in fact created items in your database, i assume you are using sqlite. you can do this in the rails console. or you can enter directly into the form or "The view to create the post" as you call it.
it seems to me that your show action in your controller and your view are correct -- at least, they should show your data.
even without the form working, your data should still show up -- if you input your data via rails console.
I have a survey model that has no index page. I only want an edit view for this model, but it seems like rails wont let me do that. It complains undefined method surveys_path when I try to use form_for(#survey). Is there anyway around doing this without creating an empty index route/view.
Here is my survey controller so far
class SurveysController < ApplicationController
def show
#survey = Survey.find(params[:id])
end
def edit
#survey = Survey.new
job = Job.find(params[:id])
#survey.job_id = job.id
authorized_user = job.user
unless !is_runner?(current_login) && current_login.id == authorized_user.id
redirect_to jobs_path
end
end
def update
#survey = Survey.new(params[:survey])
end
end
And here is the form partial being rendered in the edit.html.erb
<%= form_for(#survey) do |f| %>
<div class="field">
<%= f.label :speed %><br />
<%= f.text_field :speed %>
</div>
<div class="field">
<%= f.label :service %><br />
<%= f.text_field :service %>
</div>
<div class="field">
<%= f.label :suggestion %><br />
<%= f.text_area :suggestion %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
It shouldn't be asking about surveys_path just for defining a form, but there are some oddities in your controller code that could be causing you grief.
Your edit action should, in the surveys controller of a RESTful app, be using that params[:id] to find an existing survey, why is it finding a job? It should be doing the same as your show action.
Your survey object in the survey edit method is new and unsaved, therefore the form builder will generate a form pointing to a create action rather than an update action. Form builder will generate an edit form only for records that are persisted?
Have you defined the routes for this model? You should have something like the following in your routes file:
resources :surveys, :except => [:index] # will create all rest routes for survey model except for an index route.
You get this error if you mistakenly run your model/table as plural instead of singular. You probably ran something like this:
rails generate model surveys
Instead of this:
rails generate model survey
So, if you run:
rake routes
You'll probably see that all of your routes have an 's' ie. surveys_new.
So, rails is confused. You need to rename your table in a migration.
class ChangePluralNametoSingularforSurveys< ActiveRecord:Migration
def change
rename_table :surveys, :survey
end
end
Then you'll have to rename your files (surveys.rb, etc) manually.
For me, the issue was nested routes. If your object has a parent and its routes are nested, you'll need to do something like:
<%= form_for([#category, #survey]) do |f| %>
...
<% end %>
I am pretty new to Rails and keep getting the error below. Any help will be greatly appreciated. Please!
Error : undefined method `model_name' for NilClass:Class
Here is my memberships controller.
class MembershipsController < ApplicationController
def new
end
def create
#user = User.find_by_email(params[:email])
#project = Project.find(params[:project_id])
#membership = #project.memberships.build(project_id: #project.id, user_id: #user.id)
redirect_to project_url(#membership.project_id)
end
end
And Here is my form for membership.
It is nested underneath project routes.
<%= form_for([:project, #membership]) do |f|%>
<div class="field">
<%= f.label "Enter the email of the person you'd like to invite:" %>
<%= text_field_tag :email %>
</div>
<div class="actions">
<%= f.submit "Save", :class=>"btn btn-primary btn-large" %>
</div>
<% end %>
For your information, memberships table is a joining table between projects table and users table.
I can't figure out why I keep getting the error above. Maybe there is something I am missing. Help Please! Thanks!
One of the 2 objects #user or #project cannot be found. Though the result becomes nil. You cannot call a method on a nil object. To know better where the faulty object is log the results.
Rails.logger.debug #user.inspect
Rails.logger.debug #project.inspect
Your form should be form_for [#project, #membership] where #project is a Project instance, not :project.