I’ve used the liveview generator for a database table that will hold mandarin characters and their translation.
I can enter a character and save it to the database, but I can’t find a way to save the user_id to the table; and with liveview not using the controller concept in the same way (correct?) I can’t figure out where to make this association.
I don’t want to put it as a user facing field even though it isn’t a high stakes situation because I’d like to be able to use this same code to build more secure things later. I’ve tried to trace the logic through the code and this is what I am getting:
Start at router.ex with /mandarin_characters/new
live "/mandarin_characters/new", MandarinCharacterLive.Index, :new
This takes me to live/mandarin_character_live/index.ex with the :new action, but I don’t want to put the id in assign because it will be visible to the user right?
defp apply_action(socket, :new, _params) do
|> assign(:page_title, "New Mandarin Character")
|> assign(:mandarin_character, %MandarinCharacter{})
end
The assign appears to reference the mandarin_character struct in my Languages context /languages/mandarin_character.ex and I tried to put the user_id in the changeset but it seems like I need to have inserted it into…something…before it comes here?
schema "mandarin_characters" do
field :etymology, :string
field :final, :string
field :hanzi_standard, :string
field :hanzi_traditional, :string
field :image, :binary
field :initial, :string
field :meaning, :string
field :mnemonic, :string
field :part_of_speech, :string
field :pinyin, :string
field :pronounciation_helper, :string
field :radical, :string
field :sound, :binary
field :stroke_order_simplified, :binary
field :stroke_order_traditional, :binary
field :tone, :string
field :translated_to, :string
field :vote_value, :integer
timestamps()
belongs_to :user, Users
end
@doc false
def changeset(mandarin_character, attrs) do
mandarin_character
|> cast(attrs, [:hanzi_standard, :hanzi_traditional, :pinyin, :meaning, :pronounciation_helper, :mnemonic, :part_of_speech, :radical, :initial, :final, :tone, :etymology, :image, :sound, :stroke_order_simplified, :stroke_order_traditional, :translated_to, :user_id, :vote_value])
|> validate_required([:hanzi_standard])
end
this is the user struct that it is referencing
schema "user" do
field :email, :string
field :password, :string, virtual: true
field :hashed_password, :string
field :confirmed_at, :naive_datetime
timestamps()
has_many :mandarin_characters, MandarinCharacter
end
I can’t see where the conn
would pass in through this process, normally I would look for the controller, but that idea seems to be different for liveview.
I am thinking that I could do something in the languages/languages.ex file?
def create_mandarin_character(attrs \\ %{}) do
%MandarinCharacter{}
|> MandarinCharacter.changeset(attrs)
|> Repo.insert()
end
but I can’t see how the conn struct would pass through here either…
Thanks for any help