I have a table Cases with the has_many association to users. I’m trying to display all the cases related to a user by rendering it in user show page. I’m unable to render the case in here. Any help would be appreciated.
My code for user show page :
<table >
<tr>
<td >
<h1>
Welcome <%= current_user.first_name %> you are <%= current_user.role %>
</h1>
</td>
</tr>
<tr>
<td>
<% render @cases %>
</td>
</tr>
</table>
Show controller in users:
def show
@user = User.find(params[:id])
@cases = @user.cases
@title = @user.first_name
end
I have a file _case.html.erb in views/cases.
The problem is that
caseis a reserved word in Ruby, and when dorender @cases, it is trying to create a local variable calledcasein the partial, which is not allowed. You have to call the local variable something else. The docs here explain how with this example:In your case that would be:
And then in the case partial, just use the variable
current_casewhere you would have otherwise used the variablecase.