Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Rails 3 is it possible or proper to call @variable from another model in a view?
    primarykey
    data
    text
    <p>Here I met a problem... I am building a homepage of a website and trying to integrate infos about users, messages, location_infos bla bla bla into the same page (using jquery and ajax)...</p> <p>In PHP or JSP, I think such page is quite easy to make(divide all these functions into different services and build a homepage to load them), but in Rails 3, I found that actually I could only render @user to the view if the model is 'User' and only @user_group if the model is 'User_group'...</p> <p>Well, I use 'has_many, belongs_to,has_one' to organize some of these models. (so that I could use variables like user.user_groups.each) But actually there are always something quite different that I think it is not proper to claim these two models had a connection, for example between locations and users...So in this circumstance, how could I refer variables from other models in a certain view page? Or is this quite harmful which I really have to avoid so that I may not seriously break the RESTFul style of pages? </p> <p>Well here is my codes:</p> <pre><code>Class UsersController&lt; ApplicationController def home if session[:current_user_id]==nil redirect_to :action=&gt; "login" else @user =User.find(session[:current_user_id]) respond_to do |format| format.html format.json {render json: @user} end end end end </code></pre> <p>so in the users/home.html.erb</p> <h2> I could use codes like <code>@user.XXX</code> to generate the content</h2> <p>But if I rewrite the method as below:</p> <pre><code> def home if session[:current_user_id]==nil redirect_to :action=&gt; "login" else #here \\\ @user_2=User.find(session[:current_user_id]) respond_to do |format| format.html format.json {render json: @user} end end end </code></pre> <p>I found it impossible to call @user_2 in the users/home.html.erb</p> <p>And if I add more into the UsersController as:</p> <p>@user_group=User_group.find(session[:current_user_id])</p> <p>I found I cannot just render the <code>@user_group</code> like <code>format.html {render @user_group}</code> And I also cannot get the <code>@user_group</code> in the users/home.html.erb </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload