Munir Hoque
Munir Hoque

Reputation: 365

redirect_to an instance variable does not work in Rails 4

I'm completely new to Rails. Just started from *http://guides.rubyonrails.org/getting_started.html While trying I'm stuck with redirect_to problem like below error -

NoMethodError in SwversionsController#create
undefined method `sw_versions_url' for #<SwversionsController:0xa38d158>
Rails.root: C:/Sites/edisonprojectmanagement
Application Trace | Framework Trace | Full Trace
app/controllers/swversions_controller.rb:19:in `create'*

This error occurs after clicking submit button. I found the model is working fine and data is saved in my postgresql database. I think problem is with my redirect_to method.

My SwVersionsController code looks like -

class SwversionsController < ApplicationController
def index

end
def show
    @swversion = SwVersion.find(params[:id])

end
def new

end

def create
    @swversion = SwVersion.new(swversion_params)
    @swversion.save
    redirect_to @swversion
end

private
def swversion_params
    params.require(:swversion).permit(:sw_version, :description)
end
end

And the new.html.erb code is -

<h1> New SW versions </h1>
<%= form_for :swversion, url: swversions_path do |f| %>
<p>
 <%= f.label :sw_version, "SW Version" %> <br>
 <%= f.text_field :sw_version %>
</p>
<p>
    <%= f.label :description, "Description" %> <br>
    <%= f.text_area :description %>
</p>
<p>
    <%= f.submit "Submit" %>
</p>
<% end  %>

My routes.rb is pretty simple

Rails.application.routes.draw do resources :swversions get 'welcome/index

Someone please help me to banish this problem

Upvotes: 0

Views: 119

Answers (2)

Pavan
Pavan

Reputation: 33542

undefined method `sw_versions_url' for SwversionsController:0xa38d158

Naming Conventions:

Rails is very strict in naming and for a good reason. Rails expects the class names to be CamelCase and the methods/variables to be snake_case.

Your model class name is SwVersion, so the method/variable name should be sw_version not swversion. So you should change swversion to sw_version in your entire code

You should also change the controller class name to SwVersionsController.

And also if you have resources :swversions in routes.rb, then you should change it to resources :sw_versions

More about naming conventions here

Upvotes: 1

Uday kumar das
Uday kumar das

Reputation: 1613

Try this code:

redirect_to sw_version_path(@swversion)

Upvotes: 0

Related Questions