user3242743
user3242743

Reputation: 1851

Rails: action and views

My question is : do we need a view file for each action in our controller?

(like if we defined a say_hello action in a controller, is it necessary to add say_hello.html.erb in his view directory?

Upvotes: 2

Views: 85

Answers (4)

creativereason
creativereason

Reputation: 1524

I'll edit this to say it depends (with same content). If you plan on using that controller action as JS or JSON you don't need a view file. if you want one to share in multiple views, the file can contain a shared partial (which can be used in other views). This examples is shown by the generators scaffolding create examples like this. They are helpful if you are learning rails. Not great otherwise.

If you were to share a partial, you could have a partial named _form.html.erb and then inside your say_hello.html.erb file, it would just call:

<%= render 'form' %>

If you want to render JSON or JS files you can respond_to in your action:

   respond_to do |format|
      format.html # say_hello.html.erb
      format.json { render json: @hello } #no file needed
      format.js   { render js: @hello } 
      #format.js   {} #do nothing... or use a little javascript in there...
      # or have a file named say_hello.js.erb and use your @hello variable 
    end

Edit: One last update. Your say_hello.js.erb file can do the anything on another view (if called remotely): say_hello.js.erb

<% if @hello.attribute == "some value"  %>
    $('#div_in_another_view').show();
<% else %>
    $('#div_in_somewhere_else').hide();
<% end %>

You can do jQuery and anything you want to the view calling it (as long as it's using AJAX). End edit Guides are great place to get started. Railscasts.com as well (even though Ryan isn't updating anymore).

Edit: A great example on the different options on the respond_to is on this rails guide regarding javascript

You can just pass javascript straight from that format.js call, or use a file if you need more complicated stuff. You don't need to do anything also. You could just have it return xml or nothing as well, depending on your use case.

Upvotes: 3

Hesham
Hesham

Reputation: 2347

No, it's really up to you and it depends on what the action will actually do. Actions can render different types content types: text, json, html, xml... etc. Here's an example:

def show
  render xml: @something
end

This action doesn't have a view, but it'll output an xml when called. It can also render different things based on the format of the call:

def show
  respond_to do |format|
   format.html do
     redirect_to '/'
   end
   format.json do
     render xml: @something
   end
  end
end

The action may also redirect (again, this one doesn't have a file):

def show
  redirect_to '/'
end

At the end, it's really up to the programmer to handle how the action behaves, but if you leave it empty, it'll assume there's a file to render.

Upvotes: 0

CaptChrisD
CaptChrisD

Reputation: 195

No you do not need a view for each action. BUT you do need a view for each action that will reach the end of the method.

If you return anywhere in the action then you are fine. A view is only required when an implicit render is called due to execution reaching the end of the action.

Upvotes: 0

Rafa Paez
Rafa Paez

Reputation: 4870

No, it is not required. For example, you can render json or xml data from the controller without needed a view at all. This article explains it very well http://guides.rubyonrails.org/layouts_and_rendering.html

Upvotes: 0

Related Questions