I am building an application in which when the user log in as an admin it will have the list of registered users and have four links show,edit destroy and settings.What i want is when the admin click on the settings link it will have the view of check boxes in which admin decides the permission of users to read,edit,create and destroy of the model available in an application.
Thanks in advance.
If I understand you correctly you are looking for a view that sets the settings for one user.
Since you didn't give any details on your model I'll assume the Model is called User and the permissions are just boolean fields on that model.
Similar to this:
User
- can_read
- can_create
- can_destroy
- can_edit
I would then implement the view like this:
<%= form_for #user do |f| %>
<%= flabel :can_read %>
<%= f.check_box :can_read %>
<%= flabel :can_create %>
<%= f.check_box :can_create %>
....
<%= f.submit %>
<% end %>
Or shorter:
<%= form_for #user do |f| %>
<% [:can_read, :can_create, :can_edit, :can_delete].each do |permission| %>
<%= flabel permission %>
<%= f.check_box permission %>
<% end %>
<%= f.submit %>
<% end %>
The controller code would obviously look like this:
def edit
#user = User.find(params[:id]
end
def update
#user = User.find(params[:id]
#user.update_attributes(params[:user])
end
Related
I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.
There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.
Listing Model - belongs_to :area
Area Model - has_many :listings
I'm trying to implement a search using Ransack with check boxes; where user checks selected areas, search returns all the listings of the areas selected.
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
Console output:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
Just not sure how to implement it to accept multiple check box values.
Instead of using "area_id_eq", use "area_id_any". You'll also want to check to make sure that your parameters are selected:
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
I have a form to create adverts.
Controllers:
def edit
#engines = Engine.all
#car = Car.find(params[:id])
end
def update
#car = Car.find(params[:id])
if #car.save
redirect_to root_path
end
end
My routes:
resources :adverts
Create.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Create car!" %></div>
<% end %>
I can create advert, but I can't to update it.
edit.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Update car!" %></div>
<% end %>
when I submited my form, I have an error - No route matches [PUT] "/adverts"
$ rake routes:
adverts GET /adverts(.:format) adverts#index
POST /adverts(.:format) adverts#create
new_advert GET /adverts/new(.:format) adverts#new
edit_advert GET /adverts/:id/edit(.:format) adverts#edit
advert GET /adverts/:id(.:format) adverts#show
PUT /adverts/:id(.:format) adverts#update
DELETE /adverts/:id(.:format) adverts#destroy
I need help.
When you are updating you have to let Rails know which object you want to update by passing an id.
In edit.html.erb change:
<%= form_for #car, :url => adverts_path do |f| %>
to:
<%= form_for #car, :url => advert_path(#car) do |f| %>
By the way, I find your code very strange. Why don't your model names match your controllers and routes? I mean you are creating an advert but your model is called car. That doesn't make any sense. Either call it car or advert, but don't mix them.
If you used RESTful routing, you don't need to specify a url, just need:
<%= form_for #car do |f| %>
The form can know #car is new record, or saved record, so it will send appropriate http method.
And in your update action:
def update
#car = Car.find(params[:id])
if #car.update_attributes(params[:car])
redirect_to root_path
end
end
I got myself in a similar situation today with a mismatched resource and model name. I agree the model and controller names need to correlate, but you can override the routes name to be whatever you want.
resources :cars, path: "adverts"
Along with RESTful routing
<%= form_for #car do |f| %>
You may also want to make sure your url: path is singular on the #form_form.
Im working on a rails app where a user has the ability to upload photos. When a user uploads their photos it will appear on their profile. Everything works great besides when there is no photos created then I am unable to view the user profile page because of the photo being nil.
here is my show method in the users_controller.rb
def show
#user = User.find_by_id(:id)
#photo = #user.photos.find(params[:id])
end
Here is my show.html.erb
<% for photo in #user.photos %>
<%= photo.title %>
<%= photo.description %>
<%= image_tag photo.image_url(:thumbnail) %>
<%= link_to "Show", photo %>
<br>
<% end %>
How can I bypass this error?
The code for your action seems wrong...
It should be this:
def show
#user = User.find(params[:id])
##photo = #user.photos.find(params[:id])
end
I commented out the third line on purpose, because I'm not sure what it is you want to do there, yet.
The #user variable needs to be defined using params[:id], given that this is the show action for the UsersController, so the id for the user will be passed through as params[:id].
But then you go and use this to find the photo for the user, which is what confuses me... the Photo record's id attribute is probably not going to be the same as the User record's id attribute.
So what is it?
You can replace the code in your show.html.erb with this:
<% if #user.photos %>
<% for photo in #user.photos %>
<%= photo.title %>
<%= photo.description %>
<%= image_tag photo.image_url(:thumbnail) %>
<%= link_to "Show", photo %>
<br>
<% end %>
<% end %>
The problem is, when the user doesn't have any photos, #user.photos returns nil so you have to check for that first.
form with the single submit. How to store that values in the data base
<%= form_for (#movie) do |f| %>
//Movie
<%= f.label :moviename,"Movie Name:"%>
<%= f.text_field :moviename%>
//Releases
<%= f.fields_for :release do |release_fields| %>
<%= release_fields.label :theatre,"Theatre :"%>
<%= release_fields.text_field:theatre%>
<%= release_fields.label :city,"City :"%>
<%= release_fields.text_field:city%>
<%= release_fields.label :releasedate,"Release Date :"%>
<%= release_fields.text_field:releasedate%>
//Submit:
<%= f.submit "Save The Movie"%>
<% end %>
<% end %>
this is the form with the single submit button.There an error it showing the unknown attribute release can any on help with is issue Please
fields_for is expecting to access #movie.release, does that exist?