issues with changing directory structure in rails3

26 views
Skip to first unread message

amvis

unread,
Apr 21, 2012, 5:21:51 AM4/21/12
to rubyonra...@googlegroups.com
class BranchesController < ApplicationController
  def branch
    @branch = Branch.new (params[:branch])
    branch_back = @branch.branch
  end
end

 in the model have one branch.rb
 class Branch
    def branch
    end
 end

In the view have one branches folder, also have the branch.html.erb in that folder.
   <%= form_for(@branch) do |f| %>
   <%end%>

routes.rb

match 'branch'     => 'branches#branch'
resources :branches

i am getting this following error

NoMethodError in BranchesController#branch

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
What am I doing wrong?
Thank you
vishnu



amvis

unread,
Apr 21, 2012, 7:39:18 AM4/21/12
to rubyonra...@googlegroups.com
I have changed the code like this
class BranchesController < ApplicationController
  def branch
    @branch = Branch.new  
  end

  def create
    @branch = Branch.new(params[:branch])
  end
end
branch.html.erb in branches folder

<%= form_for(@branch) do |f| %>
 <%end%>


Now the problem is if i want to display some value from branches controller to branch.html.erb, i have to define that in branch method in BranchesController, but here  all the functionality can be done in create method in BranchesController, so that way i didn't get any values in branch.html.erb, How to do that....?. How to  write that in branch method?.. if the branch method not in BranchesController, the error will be undefined method model_name nill class, also if the create method not in BranchesController, the error will be the action create not found

What am i doing wrong
 thanks..

Colin Law

unread,
Apr 21, 2012, 7:56:02 AM4/21/12
to rubyonra...@googlegroups.com
On 21 April 2012 08:39, amvis <vgrkr...@gmail.com> wrote:
> I have changed the code like this
> class BranchesController < ApplicationController
>   def branch
>     @branch = Branch.new
>   end
>
>   def create
>     @branch = Branch.new(params[:branch])
>   end
> end
> branch.html.erb in branches folder
>
> <%= form_for(@branch) do |f| %>
>  <%end%>
>
>
> Now the problem is if i want to display some value from branches controller
> to branch.html.erb, i have to define that in branch method
> in BranchesController, but here  all the functionality can be done in create
> method in BranchesController, so that way i didn't get any values in
> branch.html.erb, How to do that....?. How to  write that in branch method?..
> if the branch method not in BranchesController, the error will be undefined
> method model_name nill class, also if the create method not
> in BranchesController, the error will be the action create not found
>
> What am i doing wrong

I don't have much idea about what problem you are trying to describe,
I presume English is not your first language and understand it can be
difficult to explain a problem clearly. If I am right then you have
an action branch in the branches controller but have the problem that
in order for data to be seen in the view then you have to setup
variables in that action. If you don't want to set them up there then
possibly a before_filter is what you want.

If I am not understanding the problem correctly then please try again
to explain it. Cut your example down to the absolute minimum code
required and show what you are trying to do and what the problem is.
After writing your description of the problem imagine yourself as
someone who knows nothing about your requirement, read your
description carefully, and make sure the description of the problem is
clear.

Colin
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/765tgLwVuLwJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

amvis

unread,
Apr 21, 2012, 8:14:10 AM4/21/12
to rubyonra...@googlegroups.com
   Thanks colin, 
   what i am trying to do,

branches_controller

class BranchesController < ApplicationController 
   def branch 
     @branch = Branch.new 
   end 
 
   def create 
     @branch = Branch.new(params[:branch]) 
     @name = "vishnu"
   end 
 end 

branch.html.erb
<h4> new  <%=@name %> </h4>
 <%= form_for(@branch) do |f| %> 
   <%= f.submit "Save" %>
  <%end%> 

 if i have to get @name in branch.html.erb, i have to define that branch action in BranchesController, am i correct...?. So in this application all the main function done in create action in BranchesController. In that way how to get that values in branch.html.erb

Vishnu
 
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to

Colin Law

unread,
Apr 21, 2012, 8:19:05 AM4/21/12
to rubyonra...@googlegroups.com
On 21 April 2012 09:14, amvis <vgrkr...@gmail.com> wrote:
> ...
>    what i am trying to do,
>
> branches_controller
>
> class BranchesController < ApplicationController
>    def branch
>      @branch = Branch.new
>    end
>
>    def create
>      @branch = Branch.new(params[:branch])
>      @name = "vishnu"
>    end
>  end
>
> branch.html.erb
> <h4> new  <%=@name %> </h4>
>  <%= form_for(@branch) do |f| %>
>    <%= f.submit "Save" %>
>   <%end%>
>
>  if i have to get @name in branch.html.erb, i have to define that branch
> action in BranchesController, am i correct...?. So in this application all
> the main function done in create action in BranchesController. In that way
> how to get that values in branch.html.erb

Use before_filter to setup @name for both create and branch.

Colin

amvis

unread,
Apr 21, 2012, 8:35:10 AM4/21/12
to rubyonra...@googlegroups.com
    Thanks
   Can i do like that,

    class BranchesController < ApplicationController  
     before_filter :branch,:create

    def branch 
      @branch = Branch.new 
    end 
 
    def create 
      @branch = Branch.new(params[:branch]) 
      @name = "vishnu" 
    end 
  end 
  
Is it correct way...? 

Colin Law

unread,
Apr 21, 2012, 8:44:57 AM4/21/12
to rubyonra...@googlegroups.com

No, have a look at the Rails Guide on Action Controller Overview to
see how to use it. Also google for
rails before_filter
to find many examples and documentation.

Colin

>
>
>>
>> Use before_filter to setup @name for both create and branch.
>>
>> Colin
>

> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/rubyonrails-talk/-/TfDKhbzW_zkJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.


> To unsubscribe from this group, send email to

> rubyonrails-ta...@googlegroups.com.

amvis

unread,
Apr 22, 2012, 10:45:19 AM4/22/12
to rubyonra...@googlegroups.com
      i have used this http://rails-bestpractices.com/posts/57-substituting-before_filter-load_object, but i didn't get that correctly, when i use the access specifier(private,protected),i am trying that for 2 days, i have got the issues...can u rearrange with the above code, for accessing instance variable in all action...

Thank you
vishnu

 

Colin

>
>
>>
>> Use before_filter to setup @name for both create and branch.
>>
>> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/TfDKhbzW_zkJ.
>

> To post to this group, send email to rubyonrails-talk@googlegroups.com.


> To unsubscribe from this group, send email to

> rubyonrails-talk+unsubscribe@googlegroups.com.

Colin Law

unread,
Apr 22, 2012, 10:49:02 AM4/22/12
to rubyonra...@googlegroups.com
On 22 April 2012 11:45, amvis <vgrkr...@gmail.com> wrote:
>
>       i have used
> this http://rails-bestpractices.com/posts/57-substituting-before_filter-load_object,
> but i didn't get that correctly, when i use the access
> specifier(private,protected),i am trying that for 2 days, i have got the
> issues...can u rearrange with the above code, for accessing
> instance variable in all action...

Show us what you tried and what happened.

Colin

Colin Law

unread,
Apr 22, 2012, 10:51:09 AM4/22/12
to rubyonra...@googlegroups.com

Just use a simple before_filter initially to get it working.

Colin

amvis

unread,
Apr 22, 2012, 10:59:04 AM4/22/12
to rubyonra...@googlegroups.com
 class BranchesController < ApplicationController  
     before_filter :create

    def branch 
      @branch = Branch.new 

         puts "name...#{@name}"  //i didn't get the @name variable 

    end 
 
    def create 
      @branch = Branch.new(params[:branch]) 
      @name = "vishnu" 
    end 
  end 

  Here the create is my calling function,i mean when i click the button that create function will call, ie Branches#create.
  What am i  wrong...?
  Thank you
  vishnu
 

Colin

Colin Law

unread,
Apr 22, 2012, 11:10:02 AM4/22/12
to rubyonra...@googlegroups.com
Don't try and call one of the actions in the filter. Add a new method
that is called from the before filter and sets up what you want, so
possibly
before_filter :setup

and

private
def setup
@name = "vishnu"
end

Then setup will be called before each action.
By putting it in the private section of the controller the setup
method is not available as an action itself, but just as a method that
can be called within the controller

Colin

amvis

unread,
Apr 23, 2012, 3:55:43 AM4/23/12
to rubyonra...@googlegroups.com
Thanks colin, thank you
when i compare this code with my application code, in the above code, before doing all the method, the setup method will run, and set the variables, so that can be accessed for all methods. But here i have one doubt, This is my application structure.....

-Myapp

     -Controller
           ->pages_controller.rb
     -View
           -pages
              ->page.html.erb

class PagesController < ApplicationController

def page
     
end

end
My doubt is if i display any variable(example is @name = vishnu) in page.html.erb, i have to  create one page action in PagesController..? or any other way to access....?

Colin Law

unread,
Apr 23, 2012, 7:44:18 AM4/23/12
to rubyonra...@googlegroups.com
On 23 April 2012 04:55, amvis <vgrkr...@gmail.com> wrote:
> ...
> Thanks colin, thank you
> when i compare this code with my application code, in the above code, before
> doing all the method, the setup method will run, and set the variables, so
> that can be accessed for all methods. But here i have one doubt, This is my
> application structure.....
>
> -Myapp
>
>      -Controller
>            ->pages_controller.rb
>      -View
>            -pages
>               ->page.html.erb
>
> class PagesController < ApplicationController
>
> def page
>
> end
>
> end
> My doubt is if i display any variable(example is @name = vishnu) in
> page.html.erb, i have to  create one page action in PagesController..? or
> any other way to access....?

Sorry, no idea what you mean.

Colin
Reply all
Reply to author
Forward
0 new messages