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
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.
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.
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
new <%=@name %>
<%= 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
> ...
> 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
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...?
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.
class BranchesController < ApplicationController
def branch
@branch = Branch.new
end
def create
@branch = Branch.new(params[:branch])
@name = “vishnu”
end
end
branch.html.erb
new <%=@name %>
<%= 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
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…?
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.
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…
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
Just use a simple before_filter initially to get it working.
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.
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
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…?